Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Pagination first page is always current page

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

like image 689
JoeMoe1984 Avatar asked Sep 22 '13 00:09

JoeMoe1984


1 Answers

define cur_page and define controller like :

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

    //...

}
like image 64
Arnab Avatar answered Sep 20 '22 21:09

Arnab