note 95374 deleted from reserved.variables.get by thiago
| From: | thiago@php.net | Date: | Wed, 30 Dec 2009 12:08:57 +0000 |
| Subject: | note 95374 deleted from reserved.variables.get by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164462@lists.php.net to get a copy of this message | ||
Note Submitter: John Galt
----
I created a function to help manage creating a hyperlink and modifying a parameter (or adding it to
the query string). It preserves the current location of the parameter in the query string if it
already exists, or appends it to the end. It does not duplicate parameters.
<?php
function changeQueryString($parameter, $value)
{
$queryString = $_SERVER["QUERY_STRING"];
if($queryString == "")
return $parameter."=".urlencode($value);
$ampExpl = explode("&", $queryString);
foreach($ampExpl as $i => $query)
{
$equExpl = explode("=", $query, 2);
$paramsAndVals[$equExpl[0]] = $equExpl[1];
if($equExpl[0] === $parameter)
$key = $i;
}
$ampExpl[$key] = $parameter."=".urlencode($value);
return implode("&", $ampExpl);
}
?>
Use it, for example, to create a hyperlink like this:
<a href="mypage.php?<?php echo htmlspecialchars(changeQueryString("pi",
"42")); ?>">Click here to change pi to 42!</a>
This will change the query string from (e.g.):
a=1&b=2&c&d&pi=17&k=12
to:
a=1&b=2&c&d&pi=42&k=12
or
a=1&b=2&c&d&k=12
to
a=1&b=2&c&d&k=12&pi=42