4

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);

2 Answers 2

15

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

$config['use_page_numbers'] = TRUE;

and it works like a charm!

Sign up to request clarification or add additional context in comments.

1 Comment

i added additional code to make the above work like what @Dale had mentioned.
4

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.

8 Comments

It seems like I have everything done correctly, from what you said. I updated my answer with the config code.
Sorry I should explain that this is the way codeigniter works, I think if you really really wanted to have it display the 'correct' page number it would require some form of hack to ci's pagination class.
Also I'm not sure about this.. $config['uri_segment'] = $this->uri->segment(2); I think what this is doing is actually setting the uri_segment to the value of $this->uri->segment(2), which could be 40987 and that wouldn't work :)
yeah I've noticed and fixed the $config['uri_segment'] to the right one. Now I got an issue, so if I'm on null page (first one), the pagination doesn't work, it is showing 50 out of 50 records at one page instead of 5 records per page... Could you help with that?
I'm getting the records with the following limit: $config['per_page'], $page * $config['per_page'] -> (LIMIT, OFFSET), see my updated answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.