Php 如何使用Codeigniter显示数据库列中特定字段的总计数?
我想在视图中显示数据库中特定的字段值 例如,我的表格:Php 如何使用Codeigniter显示数据库列中特定字段的总计数?,php,codeigniter,Php,Codeigniter,我想在视图中显示数据库中特定的字段值 例如,我的表格: id | rec_scope | rec_category -------------------------- 1 | Product | New 2 | Process |
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
我的灵感来自:
及
控制器:
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。这可能会产生您想要的结果:
$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);
你期望的输出是什么?您的表格示例在第10行有一个输入错误:Prcduct应该是Product,对吗?@Vickel是的..这只是一个输入错误..但实际上并不重要。顺便说一句,我只需要显示特定表字段的总计数值,即乘积。因此,输出还将显示类别New&Enhanced Product的总计数..Tqcheck 4这里:一个示例数组输出会更好。我想你们不只是想要产品。但例如,数组“产品”=>数组“新”=>“4”、“增强”=>“1”、“过程”=>。。。。等等?谢谢你,维克尔。你给了我一些进步的洞察力。我会做更多的研究,并再次尝试。非常感谢@Alex。这看起来像是我想要实现的。我会先尝试,然后再反馈。