【发布时间】:2021-11-28 09:54:13
【问题描述】:
目前我正在使用 Mysql 和 CodeIgniters MVC 框架在特定时间范围内检索我的数据。目前我正在使用以下模型类从状态列中检索我的数据计数。
模型类:
public function summary($st_date,$end_date){
$this->db->select("
SUM(CASE WHEN status_to = 'Draft' THEN 1 END) AS draft,
SUM(CASE WHEN status_to = 'Unpublish' THEN 1 END) AS unpublish,
SUM(CASE WHEN status_to = 'Publish' THEN 1 END) AS publish,
SUM(CASE WHEN status_to = 'Action' THEN 1 END) AS action,
);
$this->db->where('added_date >=', $st_date);
$this->db->where('added_date <=', $end_date);
return $this->db->get('crm_listings');
}
这在我的控制器类中:
$data_sum = $this->user_model->summary($startDate,$endDate)->result();
$output .= '
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>Draft</th>
<th>Unpublish</th>
<th>Publish</th>
<th>Action</th>
</tr>
';
if(isset($data_sum) && count($data_sum) > 0)
{
foreach($data_sum as $row ){
$draft = $row->draft ? $row->draft : 0;
$unpublish = $row->unpublish ? $row->unpublish : 0;
$publish = $row->publish ? $row->publish : 0;
$action = $row->action ? $row->action : 0;
$output .= '
<tr>
<td>'.$draft.'</td>
<td>'.$unpublish.'</td>
<td>'.$publish.'</td>
<td>'.$action.'</td>
</tr>
';
}
}
else
{
$output .= '<tr>
<td colspan="5">No Data Found</td>
</tr>';
}
$output .= '</table>';
echo $output;
但现在我注意到数据库中不仅有这 4 个选项,所以我想找到一种方法来使这个计数动态化,而不是我指定 SUM(CASE WHEN status_to = 'Draft' THEN 1 END) AS draft,draft 变量应该是动态的并且应该相应地反映在具有我的显示代码的控制器类中。
【问题讨论】:
标签: php mysql codeigniter