我遇到了一个问题..。
我有很多这样的状态.
$this->db->where('Pool', "1");
$this->db->where('Bedrooms >=', "3");然后是限位语句
$this->db->limit($limit, $offset);最后我的get声明
$query = $this->db->get('table-name');我的问题是,我需要在极限语句之前计算结果,才能得到没有限制的总行。所以我试了一下.
$this->db->where('Pool', "1");
$this->db->where('Bedrooms >=', "3");
$num_rows = $this->db->count_all_results();
$this->db->limit($limit, $offset);
$query = $this->db->get('table-name');这用where语句来计数我的行。但是,get语句现在获得记录,而不使用前面的语句。
它是不可见的,但是有大量的代码处理更多的where语句,并且抓取urls中的内容,所以我宁愿不执行两次数据检索来修复这个.
干杯!
发布于 2012-09-07 22:14:57
$get_data = $this->your_model->get_data();
$data = $get_data['data'];
$count = $get_data['count'];模型
function get_data($limit = 10, $offset= 0)
{
$table = 'table-name';
$where = array('Pool' => 1, 'Beedrooms >=' 3);
$return['data'] = $this->db->from($table)->where($where)->limit($limit, $offset)->get();
$return['count'] = $this->db->from($table)->where($where)->count_all_results();
return $return;
}发布于 2013-07-10 04:29:19
我知道这是个老问题,但我遇到了这个问题,想出了一个不同的解决方案。
这样做的目的是在限制和偏移之前获取db类的副本。
$this->db->where('Pool', "1");
$this->db->where('Bedrooms >=', "3");
//here we use the clone command to create a shallow copy of the object
$tempdb = clone $this->db;
//now we run the count method on this copy
$num_rows = $tempdb->from('table-name')->count_all_results();
$this->db->limit($limit, $offset);
$query = $this->db->get('table-name');发布于 2018-01-18 12:41:44
我知道这是个老问题,但我找到了一个很简单的解决办法。
//do your select, from and where
$this->db->select('your selects');
$this->db->from('your table');
$this->db->where('your where');
//get the filtered rows count
//the trick is the first empty parameter and second false parameter
$filtered_count = $this->db->count_all_results('', false);
//limit your results and get the rows
$this->db->limit($length, $start);
$results = $this->db->get()->result_array();希望它能帮到别人
https://stackoverflow.com/questions/12320249
复制相似问题