codeigniter分页 - 结果在每一页上保持不变 [英] codeigniter pagination - results remain same on every page

查看:97
本文介绍了codeigniter分页 - 结果在每一页上保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Codeigniter分页,但遇到问题。

I'm using the Codeigniter pagination, but am encountering an issue.

点击时,网址会发生变化,例如限制更新。

The url changes on click, as in the limit updates e.g. displayAllUsers/10 to displayAllUsers/15, however the results remain same.

这是我的控制器:

 public function displayAllUsers()
    {

        $this->load->model('backOfficeUsersModel');
        $data['loggedUser'] = $this->backOfficeUsersModel->get_by('username', $this->session->userdata('username'), FALSE,TRUE);

        $this->db->order_by('userid');
        $limit = 5;     // this works, I have 5 records displayed on the page
        $offset = 3;
        $this->db->limit($limit);
        $this->db->offset($offset);
        $data['users'] = $this->backOfficeUsersModel->get();

        // line bellow, prints correct result, 17 records total in database;
        $totalresults = $this->db->get('back_office_users')->num_rows();


        $this->load->library('pagination');
        $config['base_url'] = site_url('/backOfficeUsers/displayAllUsers');
        $config['total_rows'] = $totalresults;
        $config['per_page'] = 5;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config); 
        $data['main_content'] = 'users';
        $data['title'] = 'Back Office Users';
        $errorMessage = FALSE;
        $this->load->vars($data,$errorMessage);
        $this->load->view('backOffice/template');

    } // end of function displayAllUsers

任何人都可以找到我做错了?

Anyone can spot what I am doing wrong?

推荐答案

尝试做这样的偏移,

$offset = $this->uri->segment(3);
$this->db->limit(5, $offset);

这样,当您访问您的用户时,就从正确的偏移量开始。

This way when you access your users, you start from the correct offset.

让我们假设你的表中有20个用户。当你第一次访问页面时,你的$ offset变量将是空的,因为你的链接中没有尾随数字(在$ this-> uri-> segment(3))。因此,当您查询5个用户的数据库时,从0开始,因为$ offset变量为空。在这种情况下,您的代码将是:

Let's assume that you have 20 users in your table. When you first land on the page your $offset variable will be empty as there is no trailing numbers in your link (at $this->uri->segment(3)). So when you query your database for 5 users you start from 0 as the $offset variable is empty. In this circumstance your code would be:

$this->db->limit(5, 0).

从第0条记录开始获取5条记录。

Get 5 records starting from the 0th record.

但是,当您使用分页链接时,您设置的per_page变量会在您的网址末尾添加一个数字,并重新加载网页。当这种情况发生时,你的$ offset变量给定一个数字,在这种情况下,它每次增加5。
现在在这种情况下,你的代码将是

When you use the pagination links however, the per_page variable you set adds a number to the end of your url and reloads the page. When this happens, your $offset variable gets given a number, in this case, it adds 5 each time. Now in this circumstance your code would be

$this->db->limit(5,5)

含义从第5条记录开始获取5条记录。

Meaning get 5 records, starting from the 5th record.

这篇关于codeigniter分页 - 结果在每一页上保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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