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粉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:
= $DataId; ?>
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.
= $total; ?>
This is a related post that covers passing data from a controller to a view.