如何显示group_by结果以查看?
我的控制器:
public function filter_by_province($region="",$province=""){
$this->load->model('geoinfo');
$data['numberofmun']=$this->geoinfo->provinces_count($region,$province);
$this->load->view('header');
$this->load->view('filter_result_province', $data);
}
我的模特:
public function provinces_count($region,$province){
$this->db->select('*');
$this->db->from('geo_data');
$this->db->where('region', urldecode($region));
$this->db->where('province',urldecode($province));
$this->db->group_by('municipal');
$query=$this->db->get();
$result=count('$query->num_rows()');
return $result;
}
我不知道如何显示结果进行查看。
我想要这样的结果
市政名称|记录数
市政1 | 5
市政2 | 2
市政3 | 7
市政4 | 7
市政5 | 3
如何在视图中显示
最佳答案
我想这会帮助您。
型号代码
Public function provinces_count($region,$province){
$this->db->select(‘regoin’, count(municipal) AS municipal’);
$this->db->from('geo_data');
$this->db->where('region', urldecode($region));
$this->db->where('province',urldecode($province));
$this->db->group_by('municipal');
$result=$this->db->get();
return $result;
}
控制器代码
public function filter_by_province($region="",$province=""){
$this->load->model('geoinfo');
$data['numberofmun']=$this->geoinfo->provinces_count($region,$province);
$this->load->view('header');
$this->load->view('filter_result_province', $data);
}
您可以在视图中使用foreach循环来显示数据
关于mysql - 使用group_by查看计数结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28041732/