【问题标题】:Counting the group of data in a column that is dynamic计算动态列中的数据组
【发布时间】: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


    【解决方案1】:

    您应该在查询中使用 GROUP BY

    public function summary($st_date,$end_date){
        $this->db->select("
            status_to,
            SUM(1) AS sum_status
        ");
        $this->db->where('added_date >=', $st_date);
        $this->db->where('added_date <=', $end_date);
        $this->db->group_by('status_to');
        return $this->db->get('crm_listings');
    }
    

    比在控制器中:

    if(isset($data_sum) && count($data_sum) > 0){
        $output .= '<tr>';
        foreach($data_sum as $row){
            $sum = $row->sum_status ? $row->sum_status : 0;
            $output .= '<td>'.$sum.'</td>';
        }
        $output .= '</tr>';
    }
    

    如果您需要按特定顺序排列的列,只需将总数存储在这样的数组中:

    if(isset($data_sum) && count($data_sum) > 0){
        $totals = array(
            "Draft" => 0,
            "Unpublish" => 0,
            "Publish" => 0,
            "Action" => 0,
        );
        foreach($data_sum as $row){
            $totals[$row->status_to] = $row->sum_status;
        }
        $output .= '
               <tr>
               <td>'.$totals["Draft"].'</td>
               <td>'.$totals["Unpublish"].'</td>
               <td>'.$totals["Publish"].'</td>
               <td>'.$totals["Action"].'</td>
               </tr>
            ';
    }
    

    【讨论】:

    • 好的,那么如何在我的控制器类中显示它的计数?
    • 我用控制器的代码编辑了响应
    • 我试过了,它把我所有的条目都放在了草稿栏下。这就是它过去的样子 imgur.com/1BnqKhC 和现在的样子 imgur.com/a/h4G06OO
    • 你能var_dump $data_sum 结果吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    相关资源
    最近更新 更多