【问题标题】:How to Query multiple variable to show in a View in Codeigniter如何查询多个变量以显示在 Codeigniter 的视图中
【发布时间】:2019-02-01 14:31:41
【问题描述】:

我的小项目使用 Codeigniter 进行编码,我只是停留在查询要在视图中显示的特定列的总数。这是我卡住的一些代码:

型号:

function get_dailyprob()
{
    $date = date('d-m-Y');
    $q1 = $this->db->select('client')->like('created_at', $date)->group_by('client')->get('histprob')->result();
    $q2 = $this->db->like('created_at', $date)->count_all_results('histprob'); //HERE I HAVE NO IDEA HOW TO GET A TOTAL COUNT OF EACH ROWS THAT ALREADY GROUPED BY COLUMN client
    return array('dp_client' => $q1, 'dp_count' => $q2);
}

控制器:

$data['dailyprobs'] = $this->skejuler_m->get_dailyprob();

查看:

<table class="table table-hover">
<?php if ($dailyprobs['dp_count'] > 0) {
foreach ($dailyprobs['dp_client'] as $dpc): ?>
<tr>
<td><?php echo $dpc->client; ?></td>
<td><span style="font-size:16px" class="badge bg-red"><?php echo $dailyprobs['dp_count']; ?></span></td>
</tr>
<?php endforeach; ?>
<tr><td><?php } else { echo 'No Issues!'; } ?></td></tr>
</table>
  1. $dpc->client 结果一些行,每行有不同的计数(在模型中由查询过滤)
  2. $dailyprobs['dp_count'] 当前正在显示全部结果,而我需要显示按 client 分组的总计数

对不起,如果解释令人困惑。我刚刚添加了视图的图像。如图所示,美标/Grohe 和 App Sinarmas 的总数均为 2 = 4,而实际总行数为每列 1(客户端)= 2。对不起我的英语。

【问题讨论】:

    标签: php mysql arrays codeigniter


    【解决方案1】:

    它只是通过更改模型中的查询来解决:

    function get_dailyprob()
    {
        $date = date('d-m-Y');
        $query = $this->db->select('*, COUNT(*) as cnt')->like('created_at', $date)->group_by('client')->get('histprob');
        return $query->result();
    }
    

    然后在 View 中使用 foreach 获取 Count 和 Rows Result。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-09
      • 2018-09-26
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多