繁体   English   中英

如何计算CodeIgniter框架中表中的行?

[英]How do I count the rows in a table in the CodeIgniter framework?

我正在尝试计算表中的行数。 该表具有fk'city_id'。 我想计算满足某些条件的行数,并想回显该数字。

我尝试使用以下代码:

function count(){

  $citys = $this->db->get('city');

  foreach ($citys->result() as $city) {

    $this->db->where('info', array('city_city_id'=>$city->city_id) AND status_status_id==1);
    $sql = $this->db->count_all('info');

  }

  return  $sql->result;
} 

控制器:

  $data['city'] = $this->state_model->count();
  $this->load->view('sview', $data);

视图:

 <?php foreach($citys as $cities):?>
        <h4><?php echo $city ?>
      <?php endforeach;?></br></br>

在我的模型中,我尝试计算“信息”表中的city_city_id = 1和status_status_id = 1的行数。 但是我收到以下错误:

   Severity: Notice

 Message: Use of undefined constant status_status_id - assumed 'status_status_id'

 Filename: models/State_model.php

 Line Number: 98

在第98行,我有

         $this->db->where('info', array('city_city_id'=>$city->city_id) AND                    status_status_id==1);

我刚与Codeigniter合作,因此不胜感激。

提前致谢。

这应该让你去:

$this->db->where('city_id', $city_id);
$this->db->from('city');
return $this->db->count_all_results();

返回一个整数。 CodeIgniter活动记录

您不能在PHP中间使用SQL查询,这就是AND中断它的原因。 您不需要使用AND联接查询,CodeIgniter会为您完成。

你应该去哪里

    $this->db->where('info', array('city_city_id'=>$city->city_id, 
'status_status_id'=>'1'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM