Php 如何在CodeIgniter框架中计算表中的行数?
我在计算表中的行数。该表具有fk“城市id”。我想计算符合特定条件的行数,并想回显该行数 我已尝试使用以下代码:Php 如何在CodeIgniter框架中计算表中的行数?,php,sql,database,codeigniter,Php,Sql,Database,Codeigniter,我在计算表中的行数。该表具有fk“城市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 statu
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);
视图:
在第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();
返回一个整数
不能在PHP的中间使用SQL查询,这就是为什么<代码>和< /代码>中断它的原因。您不需要使用
和加入查询,CodeIgniter会为您这样做。您的where应该像
$this->db->where('info', array('city_city_id'=>$city->city_id,
'status_status_id'=>'1'));
$this->db->where('city_id', $city_id);
$this->db->from('city');
return $this->db->count_all_results();
$this->db->where('info', array('city_city_id'=>$city->city_id,
'status_status_id'=>'1'));