db->where('Bedrooms >=', "3"); 然后是-6ren">
gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:03:18 25 4
gpt4 key购买 nike

我遇到了一个问题...

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

$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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com