Lucas
Lucas

Reputation: 3715

CodeIgniter pagination url page number

From what I saw, CodeIgniter's pagination is counting the page wrong way, because I got pagination looking like this:

1 2 3 >

And its good, the problem is in the each pagination number url, except the first one:

Number 2 from the pagination has the following url:

http://my-url.com/index.php/page/1

And number 3 has the following url:

http://my-url.com/index.php/page/2

So, the number is URL is decreased by 1 everytime.

How can I solve that, so the page numbers in urls will be the same as the page numbers in the pagination?

My pagination config:

$config['per_page']    = 5;
$config['base_url']    = site_url('page');
$config['uri_segment'] = 2;
$page = $this->uri->segment(2);
$total_rows_array = $this->records->get($config['per_page'], $page * $config['per_page']); // parameters: limit, offset.
$config['total_rows']  = count($total_rows_array);

Upvotes: 4

Views: 12262

Answers (2)

Francisco Ochoa
Francisco Ochoa

Reputation: 1558

Had the same problem you have, and I added this configuration line:

$config['use_page_numbers'] = TRUE;

and it works like a charm!

Upvotes: 15

Dale
Dale

Reputation: 10469

Are you using page numbers in the pagination config?

The way CodeIgniters pagination works (by default) is that you set the number of records to show in the code, CodeIgniter works out the offset and appends that to the URL.

The reason page 1 (if you will) has no number at the end is because there is no offset, page 2 will have an offset of 1 because it is offsetting that many records. Wow this is a lot harder to explain than I thought before I started typing this answer!

:EDIT:

Also, if you are using page numbers in the pagination config, I imagine that code igniter is taking the per_page amount and multiplying it by the page number to get the real offset for the records.

Page 2 would really work out as per_page (15) * page_number (1) = 15 for the real offset.

Upvotes: 4

Related Questions