"keeping url parameters during pagination" Answer’s

0

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.

Wednesday, March 31, 2021
 
buymypies
answered 11 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :