【问题标题】:How to display count of rows based on a particular value in codeigniter [duplicate]如何根据codeigniter中的特定值显示行数[重复]
【发布时间】:2019-05-21 10:32:20
【问题描述】:

我正在寻找一种使用 CodeIgniter 根据特定列数据显示行数的解决方案。

这基本上是针对商店注册项目的。我有一个名为shopowner 的数据库表,其中有一列名为status。而这个 status 列只有 3 个值。 IE。如果状态为0,则记录正在等待批准,一旦批准,状态将变为12 拒绝。现在在我的仪表板中,我需要显示待处理、已批准和已拒绝记录的总数。谁能帮我解决这个问题,请告诉我要在模型、控制器和视图中添加什么。我对 CodeIgniter 完全陌生。提前致谢。

型号

public function count_rows($status)
    {
        $this->db->select('status, COUNT(status) as total');
        $this->db->group_by('status', $status);  
        $this->db->get('shopowner');
    }

预期的输出会是这样的

注册总数:4 待批准:2 批准:1 拒绝:1

【问题讨论】:

  • 它看起来对我来说只是返回值 return $this->db->get('showowner')->result();

标签: php mysql database codeigniter


【解决方案1】:

如果您想在一个查询中获取所有统计信息,您可以使用以下代码

 $this->db->select('status, COUNT(status) as total');
 $this->db->group_by('status');  
 $this->db->get('shopowner');

但是如果你想得到一个特定的状态计数,你必须将你的状态代码添加到 where 参数中

$this->db->where(['status' => $status]);
$result = $this->db->get('shopowner');
echo $result->num_rows();

希望对你有帮助)

private function count_rows($status) {
    $this->db->where(['status' => $status]);
    return $this->db->get('shopowner')->num_rows();
}

在你的控制器中

    $data = [
        'pending_approval' => $this->count_rows(4),
        'approved' => $this->count_rows(2),
        'rejected' => $this->count_rows(1)
    ];
    $this->load->view('your_view', $data);

你可以使用<?=$approved?>来调用$data items

【讨论】:

  • 感谢法哈德的帮助。如果我在这里选择第二个选项,如何将此参数传递给控制器​​,然后在 status=0 时将其视为 Total count |状态=1 |状态=2
【解决方案2】:

第一行代码不需要给出'status'列,只使用'$this->db->select('COUNT(*) as total');'在第一行并使用带有状态列的 where 子句,而不是在“group_by”子句中使用的“status”列。你可以试试下面的代码:

public function count_rows($status)
{
    $this->db->select('COUNT(*) as total');
    $this->db->from('shopowner');
    $this->db->where('status', $status);
    $this->db->group_by('status');  
    $this->db->get();
}

【讨论】:

    【解决方案3】:

    我已经创建了一个名为 shopowner 的数据库表,其中有 4 个列名为:idnamestatustype_name。状态列只有 3 种类型的值,就像您描述的那样。如果 status 为 0 则记录正在等待批准,一旦批准,状态将变为 1 和 2 拒绝。 type_name 表示拒绝已批准并在其中等待,type_name 的名称类型类似于拒绝已批准或等待,id 是自动生成的,名称是客户名称。

    型号

    $this->db->select('type_name, COUNT(status) as star ');
    $this->db->from('shopowner');
    $this->db->group_by('status');
    

    控制器

    $data['user']=$this->your_model->name();
    $this->load->view('your_view', $data);
    

    查看

    <table>
      <tr>
    <th>name</th>
    <th>total</th>
    </tr>
        <?php foreach( $user as $users ){
            ?>
         <tr>
        <td><?php echo $users->type_name?></td>
        <td><?php echo $users->star?></td>
        </tr>
        <?php } ?>
      </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 2017-01-10
      • 2018-04-02
      • 1970-01-01
      相关资源
      最近更新 更多