Your model method isn't broken, but it can be refined. You should remove the condition, the loop, and use method chaining to return an array of zero or more arrays to the controller.
public function pengunjungHariIni(): array
{
return $this->db
->group_by('ip')
->get_where('tbcounter', ['date' => date('Ymd')])
->result_array();
// "SELECT * FROM `tbcounter` WHERE `date` = '20250306' GROUP BY `ip`"
}
Your mcrud model name indicates that your model class doesn't have a narrow topical concern. A well designed model class will not be used to access all database tables -- "god classes" are a bad idea for maintainability. Model classes should be logically devoted to a specific subset of data retrieval tasks in the context of your application.
We only see one method here, but maybe your model pertains to traffic or users or visitors. Maybe your model class and filename should be Visitor_model (Jumlah_model). I'll adjust the controller method with this in mind.
$this->load->model('Jumlah_model', 'JumlahModel');
$this->load->view(
'layout/wrapper',
[
'pengunjung' => $this->JumlahModel->pengunjungHariIni(),
'isi' =>'user/monitoring',
]
);
In your view layer, the $pengunjung array will be unconditionally defined as an array. Use a foreach() loop to access the data from each row and present whatever data you wish.
<div class="pull-left">Today: </div>
<div class="pull-right number">
<?php
if (!$pengunjung) {
echo "Tiada pelawat lagi"; // no visitors yet
} else {
echo '<table>'
foreach ($pengunjung as $row) {
printf('<tr><td>%s</td><td>%d</td></tr>', $row['ip'], $row['jumlah']);
}
echo '</table>';
}
?>
</div>
For those who haven't translated the Malay to English, jumlah is amount, pengunjung is visitors, and hari ini is today.
$this->mcrud->jumlah_visitor()returns an array as you have defined over there in your method, treat it as such, loop over when needed