我正在构建一个搜索功能,我很确定正确的方法是只获取您想要显示的结果数,在我的例子中每页 20 个。我想知道改变这种情况的最佳方法是什么 select('*')
返回结果总数的语句 select ('count(*)');
并重新运行完全相同的查询?
$this->db->select('*');
$this->db->from('MyTable');
$this->db->where('some complex conditions');
$this->db->join('a bunch of joins);
$this->db->limit('20','$page*20');
至
$this->db->select('count(*)');
/* reuse this part */
$this->db->from('MyTable');
$this->db->where('some complex conditions');
$this->db->join('a bunch of joins);
/* reuse this part */
运行第一个查询
$q1 = $this->db->get()->result_array();
运行第二个查询
$q2 = $this->db->get('q1 with different select')->result_array();
array_merge($q1,$q2);
最佳答案
Codeigniter 实际上有一个非常有用的分页助手: https://ellislab.com/codeIgniter/user-guide/libraries/pagination.html
如果您不喜欢使用它,您可以通过 url 将您当前所在的查询编号传递给 Controller 。
例如 www.example.com/page/搜索/40。然后查询将使用该数字作为限制。
关于php - Codeigniter 更改查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28571819/