0

so here is my model codes:

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();
}

and this is my code in html:

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

i already call the function to my controller as DataId, and i get error that Undefined array key "total"

can you guys tell me what is wrong in it?

5
  • If you var_dump the $query->result_array(); what do you get? Commented May 20, 2022 at 7:30
  • an error that undefined array key "total" Commented May 20, 2022 at 7:31
  • No, I mean if you var dump the result of the getId function. That cant give that error, since you don't ask for that key there. I mean what does the data actually look like in the $DataId? Try var dumping $DataId without any keys in there. Commented May 20, 2022 at 7:36
  • We see your view and model code, but not your controller. Please show how you pass your data from the controller to the view. getId() is a poor name for a method that returns a count. Commented May 20, 2022 at 8:17
  • @Hiro I believe this may be a misunderstanding between what result_array() returns and what row_array() returns. Either try row_array() in the model, $DataId[0]['total'] in the view, or clean up everything with the advice in my answer. Commented May 20, 2022 at 9:49

2 Answers 2

0

Some untested advice:

Your model can be refined to:

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

Your controller should call for 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 variable that associates with the first-level key in the passed in array.

<?= $total; ?>

Here is a related post which speaks on passing data from the controller to the view.

Sign up to request clarification or add additional context in comments.

Comments

0

Replace the result_array() at the model with

num_rows() 

and you can delete ['total'] from your code in html, or like this:

<?= $DataId; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.