Codeigniter Pagination first page is always curren

2019-06-19 02:01发布

问题:

I am pulling my hair out on this one. I am using CodeIgniters pagination library and right now it is always stuck on page 1 as the current page. I have checked a bunch of StackOverflow questions and I don't have the same problem as anyone else.

Here is my url structure

website.com/leaders/page/[page_number]

Here is the pagination code in my controller

    $this->load->library('pagination');

    $config['per_page'] = $query_config['limit'];
    $config['base_url'] = base_url() . 'leaders/page/';
    $config['total_rows'] = 2000; // I actually use a function for this number
    $config['full_tag_open'] = '<div id="paginate">';
    $config['full_tag_close'] = '</div>';
    $config['first_link'] = '&laquo; First';
    $config['last_link'] = 'Last &raquo;';
    $config['use_page_numbers'] = true;
    $config['uri_segment'] = 3;

    $this->pagination->initialize($config);

When I echo the pagination in the view it looks like it works. The urls on each link is correct and everything looks fine. The last link shows the last page url and the current page is 1. However when I click on page 2 or any other page from the pagination, it still shows page 1 as the current page even though the url is as follows

website.com/leaders/page/2

I used the $this->uri->segment(3) to grab the page number for my database queries so the page number is in the right segment. Just to double check I set the $config['uri_segment'] values to 1,2,3,4,5,6 just to make sure.

I found out the problem while writing this but I am still confused

Then I thought maybe there is something going on with the url itself as I have a route directing it to the index method in the controller. Here is what my routes file looks like

routes.php

$route['leaders/page/(:num)'] = 'leaders/index';
$route['leaders/page'] = 'leaders/index';

Then I tried setting the base_url for the pagination config to send it to the index directly like so:

$config['base_url'] = base_url . 'leaders/index';

Now it seems to be working properly. But how do I make it so that it works with the url structure I had before? I just think it looks nicer and I don't really need a method in the controller for this. Is there something conflicting in my routes.php file?

Thanks

回答1:

define cur_page and define controller like :

public function index($page=''){
    //...
    $page = ($page!='')? $page : 0;
    $config["cur_page"] = $page;

    //...

}


回答2:

Use this in your code, hope this will work-

if ($this->uri->segment(3) > 0) {
     $offset = $this->uri->segment(3) * $config['per_page'] - $config['per_page'];
 } else {
     $offset = $this->uri->segment(3);
 }


回答3:

I've been working for about 6 hours to make CI pagination work as I expected, and I don't know if is the order of the config elements or just my browser joking with me.

Below is my configuration array for pagination to work properly.

As you can see this is normal code, but my problem was, when I rendered for the first time, my pagination view, all seems to be ok, but if $config['per_page'] = 10; was set to 10, when I clicked on 11 link of pagination links, link number 2 showed Cseguimiento/buscar_reportes/# and looks like current page was 2 not 11.

I was very tired and I started to change the order of $config array, and suddenly it worked. SO I preesnt it here.

$config['base_url'] = base_url().'Cseguimiento/buscar_reportes/';
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$config['next_link'] = '&gt;';
$config['prev_link'] = '&lt;';
$config["full_tag_open"] = '<ul class="pagination">';
$config["full_tag_close"] = '</ul>';
$config["first_tag_open"] = '<li>';
$config["first_tag_close"] = '</li>';
$config["last_tag_open"] = '<li>';
$config["last_tag_close"] = '</li>';
$config["next_tag_open"] = '<li>';
$config["next_tag_close"] = '</li>';
$config["prev_tag_open"] = "<li>";
$config["prev_tag_close"] = "</li>";
$config["cur_tag_open"] = "<li>";
$config["cur_tag_close"] = "</li>";
$config["num_tag_open"] = "<li>";
$config["num_tag_close"] = "</li>";
$config['total_rows'] = $this->mseguimiento->filas($fecha_inicio,$fecha_fin);
$config['per_page'] = 10;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$page = $config['uri_segment'] * $config['per_page'];
$this->pagination->initialize($config);
$offset = ($this->uri->segment(3)-1)*$config['per_page'];
$output = array(
    'pagination_link'  => $this->pagination->create_links(),
    'lista_reportes'   => $this->mseguimiento->fetch_details($this->pagination->per_page, $offset,$fecha_inicio,$fecha_fin)
    );