首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在添加进一步的限制语句CodeIgniter之前对行进行计数?

在添加进一步的限制语句CodeIgniter之前对行进行计数?
EN

Stack Overflow用户
提问于 2012-09-07 14:38:38
回答 7查看 10.8K关注 0票数 10

我遇到了一个问题..。

我有很多这样的状态.

代码语言:javascript
运行
复制
$this->db->where('Pool', "1");
$this->db->where('Bedrooms >=', "3");

然后是限位语句

代码语言:javascript
运行
复制
$this->db->limit($limit, $offset);

最后我的get声明

代码语言:javascript
运行
复制
$query = $this->db->get('table-name');

我的问题是,我需要在极限语句之前计算结果,才能得到没有限制的总行。所以我试了一下.

代码语言:javascript
运行
复制
$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中的内容,所以我宁愿不执行两次数据检索来修复这个.

干杯!

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2012-09-07 22:14:57

代码语言:javascript
运行
复制
$get_data = $this->your_model->get_data();
$data     = $get_data['data'];
$count    = $get_data['count'];

模型

代码语言:javascript
运行
复制
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;
}
票数 5
EN

Stack Overflow用户

发布于 2013-07-10 04:29:19

我知道这是个老问题,但我遇到了这个问题,想出了一个不同的解决方案。

这样做的目的是在限制和偏移之前获取db类的副本。

代码语言:javascript
运行
复制
$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');
票数 18
EN

Stack Overflow用户

发布于 2018-01-18 12:41:44

我知道这是个老问题,但我找到了一个很简单的解决办法。

代码语言:javascript
运行
复制
//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();

希望它能帮到别人

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12320249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档