Gunakan CASE
pernyataan alih-alih beberapa kueri
Model:
public function summary($st_date,$end_date){
$this->db->select("
SUM(CASE WHEN status = 'D' THEN 1 END) AS draft,
SUM(CASE WHEN status = 'N' THEN 1 END) AS unpublish,
SUM(CASE WHEN status = 'Y' THEN 1 END) AS publish"
);
$this->db->where('added_date >=', $st_date);
$this->db->where('added_date <=', $end_date);
return $this->db->get('crm_listings');
}
Lihat:
Jangan membuat HTML di pengontrol karena itu praktik yang buruk di MVC. Gunakan foreach
loop dalam file tampilan untuk menampilkan nilaiBaca selengkapnya dari Tampilan CI
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>Draft</th>
<th>Unpublish</th>
<th>Publish</th>
</tr>
<?php
if(isset($data) && count($data) > 0){
foreach($data as $row ){ ?>
<tr>
<td><?= $row->draft ?></td>
<td><?= $row->unpublish ?></td>
<td><?= $row->publish ?></td>
</tr>
<?php } //Foreach end here
} else { ?>
<tr>
<td colspan="5">No Data Found</td>
</tr>
<?php } ?>
</table>
Baca selengkapnya tentang Pernyataan Kasus MySQL