Akhilesh Sharma Akhilesh Sharma - 6 months ago 21
PHP Question

Codeigniter Pagination

There is a bit of problem in my code which i am not able to solve.
I m using CI 1.7.2.
I have implemented the CI Pagination into the system correctly.
The results are displayed fine but the links in the pagination are not rendering correctly.

For eg. If i click on the page 2 then the results are displayed as per the 2nd Page but the current link at pagination numbers remains 1 which should change to 2.

Here is the code that has been implemented

$total = $this->bmodel->countResultsBanner();

$data['total'] = $total;

$uri_segment = $this->uri->segment(4);

if($uri_segment == 0 || empty($uri_segment)){
$uri_segment = 0;
}

$perPage = 5;



$config['base_url'] = base_url()."index.php/modules/banner/index";

$config['total_rows'] = $total;

$config['per_page'] = $perPage;

$config['num_links'] = 4;

//$config['cur_tag_open'] = '<b><span class="current_page">';

//$config['cur_tag_close'] = '</span></b>';

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

$result = $this->bmodel->getAllBanners($perPage,$uri_segment);


$data['result'] = $result;


thanks in advance.

J

Answer

Heyy,

I also faced the same problem. In the end, solution turned out to be very simple. :)

by default CI assumes that uri segment used for pagination is (3). Which in your case, for you (i am assuming shamelessly) is incorrect.

$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;

$config['uri_segment'] = 3; /* segment of your uri which contains the page number */

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

Hope this solves your problem