Gebruik CASE
verklaring in plaats van meerdere zoekopdrachten
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');
}
Bekijk:
Maak geen HTML in de controller omdat dit een slechte gewoonte is in MVC. Gebruik foreach
loop in view-bestand om waarden weer te gevenLees meer over van CI-weergaven
<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>
Lees meer over MySQL Case Statement