This Function will help you to manage your GET parameters to facilitate coding and prevent duplication. This is a basic version but it can be easily extended.
<?php
public function fixGet($args) {
if(count($_GET) > 0) {
if(!empty($args)) {
$lastkey = "";
$pairs = explode("&",$args);
foreach($pairs as $pair) {
if(strpos($pair,":") !== false) {
list($key,$value) = explode(":",$pair);
unset($_GET[$key]);
$lastkey = "&$key$value";
} elseif(strpos($pair,"=") === false)
unset($_GET[$pair]);
else {
list($key, $value) = explode("=",$pair);
$_GET[$key] = $value;
}
}
}
return "?".((count($_GET) > 0)?http_build_query($_GET).$lastkey:"");
}
?>
To test, copy+paste the following code into testFixGet.php
<?php
$cases = array (
0 => array("s" => 1, "fi" => 2, "m" => 4, "p" => 3),
1 => array("s" => "", "fi" => "", "m" => 4, "p" => 3),
);
$test[0] = array(
"s" => "fi=2&m=4&p=3",
"s&m" => "fi=2&p=3",
"s=4" => "s=4&fi=2&m=4&p=3",
"s=2&m" => "s=2&fi=2&p=3",
"s=&m=3" => "s=&fi=2&m=3&p=3",
"s=2&m=" => "s=2&fi=2&m=&p=3",
"s=2&m:=" => "s=2&fi=2&p=3&m=",
"z=9" => "s=1&fi=2&m=4&p=3&z=9",
"z:" => "s=1&fi=2&m=4&p=3&z",
"s:&m=3" => "fi=2&m=3&p=3&s",
"s&m=3" => "fi=2&m=3&p=3",
);
$test[1] = array(
"s" => "fi=&m=4&p=3",
"s&m" => "fi=&p=3",
"s=4" => "s=4&fi=&m=4&p=3",
"s=2&m" => "s=2&fi=&p=3",
"s=&m=3" => "s=&fi=&m=3&p=3",
"s=2&m=" => "s=2&fi=&m=&p=3",
"s=2&m:=" => "s=2&fi=&p=3&m=",
"z=9" => "s=&fi=&m=4&p=3&z=9",
"z:" => "s=&fi=&m=4&p=3&z",
);
foreach($cases as $x => $value) {
echo "<hr> CASE: $x <hr>\n";
foreach($test[$x] as $arg => $expected) {
$_GET = $cases[$x];
$res = myForm::fixGet($arg);
echo (($res === "?".$expected)?"OK":"NG ($res)")." [$arg]<br>\n";
}
}
?>