【问题标题】:How to display total count of specific field from database column using Codeigniter?如何使用 Codeigniter 显示数据库列中特定字段的总数?
【发布时间】: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


【解决方案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);

【讨论】:

  • 太棒了。非常感谢@Alex。它看起来像我想要实现的目标。我会先尝试,稍后再反馈。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
  • 2017-10-05
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
相关资源
最近更新 更多