Question

I have a pagination system that requires the page number to be in the URL. The problem is that links to the next pages grab the 'current url' and this URL already contains the page number from a previous page, for example:

the pagination links grab the current URL http://localhost/search/ and append their page number, result: http://localhost/search/1, http://localhost/search/2 etc. at the next page the following pagination links grab the current URL which might have a page number already in them! And so it appends again http://localhost/search/1/2 and everything breaks.

I really don't want to use sessions for this. Using CodeIgniter base_url() does not return the controller and function that I need and if I use $_SERVER['PHP_SELF'] then it also includes the domain. Any ideas how I might achieve this?

EDIT

Sample Code:

foreach ($reviews as $page){
        echo "<li><a href='".current_url()."/$page'>$page</a></li>";
}

echo '</ul>';
Was it helpful?

Solution

You can replace your Link with a relative one:

echo "<li><a href='../$page'>$page</a></li>";

This assumes that the page is the last part of the URL and will need special handling if there's no page specified (and 1 is assumed).

Possible ways to handle that are

  • using an if to see if a page was specified (if yes, use a relative link, if not simply append the next page number like you did until now).

  • simply linking to the first page explicitly by appending /1 when searching.

OTHER TIPS

From the CI documentation here:

http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

Could you do something like this?

foreach ($reviews as $page){
        echo "<li><a href='".site_url("url/to/results")."/$page'>$page</a></li>";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top