php - 在添加进一步的限制语句 CodeIgniter 之前计算行数?

标签 php database codeigniter

我遇到了一个问题...

我有一堆这样的语句......

$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 语句起作用。

它是不可见的,但是有大量代码处理更多的 where 语句,并在 url 中抓取东西,所以我不想为了修复这个问题而执行两次数据检索......

干杯!

最佳答案

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

想法是在限制和偏移量之前获取数据库类的副本。

$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');

关于php - 在添加进一步的限制语句 CodeIgniter 之前计算行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12320249/

相关文章:

基于字符串出现的 PHP 自定义排序函数?

php - $wpdb->准备语法混淆

mysql - 如何同步表更新

c++ - 用于 mysql 的异步 C++ 连接器

php - PHP解析/语法错误;以及如何解决它们

javascript - 解析云: Unable to return value of query of max value in a column

php - 在预 Controller codeigniter Hook 中访问 CI session

php native 查询到codeigniter查询

php - 显示具有多个需要累加的值的左连接

php - 将图像从 android 上传到 PHP 服务器