Question

My current product pagination links look like this:

http://ecommerce.site/products/category/sub-category/page/5/per_page/9

I would like to build them like this:

http://ecommerce.site/products/category/sub-category?page=5&per_page=9

So as to separate pagination from the URL and be able to tell search engines what to do with each parameter.

My current pagination config is this:

$config['per_page'] = $per_page;
$config['base_url'] = base_url().'/products/'.$data['category']->slug.'/'.$data['sub-category']->slug.'/';
$config['prefix'] = 'page/';
$config['suffix'] = ($per_page != 9 ? '/per_page/'.$per_page : '').'?'.http_build_query($_GET, '', "&");
$config['uri_segment'] = '4';
$config['use_page_numbers'] = TRUE;
$config['num_links'] = '3';
$config['prev_link'] = '‹';
$config['next_link'] = '›';
$config['first_link'] = '«';
$config['last_link'] = '»';
$config['total_rows'] = $data['products_total'];

Edit

kumal_v suggested to use the page_query_string parameter which I had missed in CodeIgniter's pagination documentation, I've answered the question with my current config.

Was it helpful?

Solution

This is what I ended up doing

Enabled query strings for pagination and changed the parameter that contains the page number (by default it is set to 'per_page')

$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page';

Set the base URL for the pagination links

$config['base_url'] = base_url().'/products/'.$data['category']->slug.'/'.$data['sub-category']->slug;

I use additional parameters which I preserve in the pagination URLs, this is to avoid recursively including the page parameter. Also it will keep the parameters for the first_url which by default will have none.

$query_string = $_GET;
if (isset($query_string['page']))
{
    unset($query_string['page']);
}

if (count($query_string) > 0)
{
    $config['suffix'] = '&' . http_build_query($query_string, '', "&");
    $config['first_url'] = $config['base_url'] . '?' . http_build_query($query_string, '', "&");
}

Set the rest of the pagination config

$config['per_page'] = $per_page;        
$config['uri_segment'] = '4';
$config['use_page_numbers'] = TRUE;
$config['num_links'] = '3';
$config['prev_link'] = '‹';
$config['next_link'] = '›';
$config['first_link'] = '«';
$config['last_link'] = '»';
$config['total_rows'] = $data['products_total'];

Optional Modify / extend the pagination library to use '?' instead of '&'.

Replace this line

$this->base_url = rtrim($this->base_url).'&amp'.$this->query_string_segment.'=';

With this line

$this->base_url = rtrim($this->base_url).'?'.$this->query_string_segment.'=';

This works in this case because I know 'page' will always be the first parameter as I'm not using enable_query_strings.

If you don't want to modify / extend the pagination library, you will have to add the '?' to the $config['base_url']. The pagination URLs will have both '?' and '&' like '?&page=6'.

Sources

kumar_v's answer

CodeIgniters pagination documentation

Preserving GET URL parameters

Getting pagination to use '?' instead of '&'

OTHER TIPS

First enable query string in pagination

$config['page_query_string'] = TRUE;
$config['enable_query_strings'] = TRUE;

Then change pagination url sufix as:

$config['suffix'] = "?page=".$yourcurrent_page_no."&per_page=".$per_page;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top