Assume this is my current URL:
http://example.com/search?param1=foo¶m2=bar
Now I want to add param3=baz
. So this is the code:
<a href="?param3=baz" >add param3</a>
//=> http://example.com/search?param3=baz
See? param1
and param2
will be removed. So I have to handle it like this:
<?php
$query_str = parse_url($url, PHP_URL_QUERY);
parse_str($query_str, $query_params);
$query_params = $query_params == '' ? '?' : $query_params . '&';
?>
<a href="?{$query_params}param3=baz" >add param3</a>
Seems ugly, but ok, nevermind. Now what happens if a parameter be already exist and I need to edit it? Assume this example:
http://example.com/search?param1=foo¶m2=bar
Now how can I make a link which edits the value of parame2
? (plus keeping other parameters)