Codeigniter分页:两次运行查询? [英] Codeigniter Pagination: Run the Query Twice?

查看:85
本文介绍了Codeigniter分页:两次运行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用codeigniter和分页类.这是一个基本的问题,但是我需要确保我没有丢失任何东西.为了获得必要的配置项目以对从MySQL数据库中获取结果进行分页,基本上需要两次运行查询,对吗?

I'm using codeigniter and the pagination class. This is such a basic question, but I need to make sure I'm not missing something. In order to get the config items necessary to paginate results getting them from a MySQL database it's basically necessary to run the query twice is that right?

换句话说,您必须先运行查询以确定记录总数,然后才能进行分页.所以我这样做:

In other words, you have to run the query to determine the total number of records before you can paginate. So I'm doing it like:

执行此查询以获取结果数量

Do this query to get number of results

$this->db->where('something', $something);
$query = $this->db->get('the_table_name');
$num_rows = $query->num_rows();

然后,我将不得不再次执行此操作以获取具有限制和偏移量的结果.像这样:

Then I'll have to do it again to get the results with the limit and offset. Something like:

$this->db->where('something', $something);
$this->db->limit($limit, $offset);
$query = $this->db->get('the_table_name');
if($query->num_rows()){

    foreach($query->result_array() as $row){

         ## get the results here
    }
}

我只是想知道我是否真的在这样做,因为查询总是需要运行两次?我正在使用的查询比上面显示的要复杂得多.

I just wonder if I'm actually doing this right in that the query always needs to be run twice? The queries I'm using are much more complex than what is shown above.

推荐答案

是的,您必须运行两个查询,但是$this->db->count_all('table_name');是一个&线更干净.

Yeah, you have to run two queries, but $this->db->count_all('table_name'); is one & line much cleaner.

这篇关于Codeigniter分页:两次运行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆