Home  >  Q&A  >  body text

How to display count data to Html using php codeigniter

This is my model code:

public function GetId()
{
    $this->db->select('count(*) as total');
    $this->db->from($this->table);
    $this->db->where('dibaca', null);
    $query = $this->db->get();
    return $query->result_array();
}

This is my html code:

<?= $DataId['total']; ?>

I have called the function as DataId to my controller, I get the error, undefined array key 'total'

Can you tell me what's wrong?

P粉668113768P粉668113768264 days ago426

reply all(2)I'll reply

  • P粉842215006

    P粉8422150062024-02-22 13:46:56

    Replace result_array() in the model with

    num_rows()

    You can remove ['total'] from the html code, or like this:

    reply
    0
  • P粉210405394

    P粉2104053942024-02-22 11:12:15

    Some untested suggestions:

    Your model can be refined into:

    public function countNullDibaca(): int
    {
        return $this->db
            ->where("dibaca", null)
            ->count_all_results($this->table);
    }

    Your controller should call the model data and pass it to the view.

    public function myController(): void
    {
        $this->load->model('my_model', 'MyModel');
        $this->load->view(
            'my_view',
            ['total' => $this->MyModel->countNullDibaca()]
        );
    }

    Finally, your view can access the variables associated with the first-level keys in the passed array.

    This is a related post that covers passing data from a controller to a view.

    reply
    0
  • Cancelreply