dqgo99177 2019-02-01 14:31
浏览 71

如何查询多个变量以在Codeigniter中的视图中显示

I have my little project coding with Codeigniter, I am just stuck in querying the total count of specific column to show in a View. Here are some pieces of my code where I get stuck:

Model:

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);
}

Controller:

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

View:

<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 results some rows and each row has different count (filtered by query in Model)
  2. $dailyprobs['dp_count'] is currently showing the whole results, whereas I need showing a total count grouped by client

Current Result

Sorry if the explanation was confusing. I just added an image of View. As shown in the picture, both American Standard / Grohe & App Sinarmas have each 2 in total number = 4, whereas the actual total row is 1 of each column (client) = 2. I am sorry for my English.

  • 写回答

1条回答 默认 最新

  • duanmuybrtg1231 2019-02-01 21:09
    关注

    It just solved by changing the query in Model:

    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();
    }
    

    Then use foreach in View to get Count and Rows Result.

    评论

报告相同问题?

悬赏问题

  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)