我想在“查看来自数据库的特定字段值”中显示。
例如我的桌子:
id | rec_scope | rec_category
--------------------------
1 | Product | New
2 | Process | Enhance
3 | Process | New
4 | System | Enhance
5 | Product | New
6 | Process | New
7 | Product | Enhance
8 | System | New
9 | Product | New
10 | Prcduct | New
我从以下方面得到灵感
COUNT / GROUP BY with active record?和Count and display how many of specific column result
控制器:
public function index()
{
$data['records_count']= $this->dashboard_m->records_count();
$data['records_scope']= $this->dashboard_m->records_scope();
$this->template->load('template', 'dashboard', $data);
}
型号:
public function records_scope() {
$this->db->select('rec_scope, COUNT(rec_scope) as total');
$this->db->group_by('rec_scope');
$this->db->order_by('total', 'desc');
$query = $this->db->get('records');
foreach ($query->result() as $row)
$data[] = array(
'rec_scope' => $row->rec_scope,
'total' => $row->total
);
return $data;
}
如何在html视图中仅显示具有特定值的特定字段的总数?谢谢。
示例:
产品总数为5。
新产品总数为4。
“增强”产品的总分类类别为1。
答案 0 :(得分:1)
这可能会产生您想要的结果:
$this->db->select('*, COUNT(rec_scope) as count');
$this->db->from('test');
$this->db->group_by('rec_category, rec_scope');
$this->db->order_by('rec_scope');
$res = $this->db->get()->result();
$data = array();
foreach ($res as $item) {
// initialize total
if (!isset($data[$item->rec_scope]['Total'])) {
$data[$item->rec_scope]['Total'] = 0;
}
$data[$item->rec_scope]['Total'] += $item->count;
$data[$item->rec_scope][$item->rec_category] = $item->count;
}
echo '<pre>';
print_r($data);