【发布时间】:2019-05-11 12:44:37
【问题描述】:
我想在查看数据库中的特定字段值中显示。
比如我的桌子:
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。
【问题讨论】:
-
你想要的输出是什么?您的表格示例在第 10 行有一个错字:
Prcduct应该是Product,对吧? -
@Vickel 是的..它只是一个错字..但实际上并不重要。顺便说一句,我只需要显示特定表字段的总计数值,即产品。因此输出还将显示类别“新”和“增强”产品的总数..Tq
-
一个示例数组输出会很好。我假设你不只是想要产品。但是例如
array('product' => array('new => '4', 'enhance' => '1'), ('process' => .... etc )? -
谢谢@Vickel。你给了我一些关于进步的见解。我会研究更多,然后再试一次
标签: php codeigniter