Viewed   89 times

Is there any way to keep my GET parameters when paginating.

My problem is that I have a few different urls i.e



How in my pagination class am I supposed to create a link to the page with a different page number on it but yet still keep the other parts of the url?



In short, you just parse the URL and then you add the parameter at the end or replace it if it already exists.

$parts = parse_url($url) + array('query' => array());
parse_str($parts['query'], $query);
$query['page'] = $page;
$parts['query'] = http_build_str($query);
$newUrl = http_build_url($parts);

This example code requires the PHP HTTP module for http_build_url and http_build_str. The later can be replaced with http_build_query and for the first one a PHP userspace implementation exists in case you don't have the module installed.

Another alternative is to use the Net_URL2 package which offers an interface to diverse URL operations:

$op = new Net_URL2($url);
$op->setQueryVariable('page', $page);
$newUrl = (string) $op;

It's more flexible and expressive.

Saturday, November 5, 2022

This should be more than enough to get you started at least

$count = 7; // number to show
// start at half threshold down from the current location.
$number = $current - round($count/2); 
if( $number > 1 ) echo '...';
else $ // increase to have number start at 1.
for( $number; $number < $number + $count; $number++)
    // your for loop as normal
if( $number < $total ) echo '...';
Sunday, September 25, 2022

Move these urls out of for loop:

echo "<ul class='pagination'>";
echo "<li><a href='index.php?page=".($page-1)."' class='button'>Previous</a></li>"; 

for ($i=1; $i<=$total_pages; $i++) {  
    echo "<li><a href='index.php?page=".$i."'>".$i."</a></li>";

echo "<li><a href='index.php?page=".($page+1)."' class='button'>NEXT</a></li>";
echo "</ul>";               
Wednesday, August 10, 2022

To answer my own question 4 years later, after having learned a lot. Especially that you shouldn't use jQuery for everything. I've created a simple module that can parse/stringify a query string. This makes it easy to modify the query string.

You can use query-string as follows:

// parse the query string into an object
var q = queryString.parse(;
// set the `row` property
q.rows = 10;
// convert the object to a query string
// and overwrite the existing query string = queryString.stringify(q);
Wednesday, October 5, 2022


This edit improves and explains the answer based on the comments.

var search =;
JSON.parse('{"' + decodeURI(search).replace(/"/g, '\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}')


Parse abc=foo&def=%5Basf%5D&xyz=5 in five steps:

  • decodeURI: abc=foo&def=[asf]&xyz=5
  • Escape quotes: same, as there are no quotes
  • Replace &: abc=foo","def=[asf]","xyz=5
  • Replace =: abc":"foo","def":"[asf]","xyz":"5
  • Suround with curlies and quotes: {"abc":"foo","def":"[asf]","xyz":"5"}

which is legal JSON.

An improved solution allows for more characters in the search string. It uses a reviver function for URI decoding:

var search =;
JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g,'":"') + '"}', function(key, value) { return key===""?value:decodeURIComponent(value) })


search = "abc=foo&def=%5Basf%5D&xyz=5&foo=b%3Dar";


Object {abc: "foo", def: "[asf]", xyz: "5", foo: "b=ar"}

Original answer

A one-liner:

JSON.parse('{"' + decodeURI("abc=foo&def=%5Basf%5D&xyz=5".replace(/&/g, "","").replace(/=/g,"":"")) + '"}')
Saturday, October 1, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :