Forum Moderators: coopster
The problem is that at times I have to change something in the string but leave the rest of it intact.
This usually causes problems with the structure of the string and/or the order because:
- the & symbol is absent
- the & symbol needs to be put back in
- I need to change part of the string but not the other part.
I am using $_SERVER['QUERY_STRING'] to find the current string. Is there a recognised method for this or is it a case of using regular expressions to replace or insert stuff?
but have to change it to page=5&artist=10.
Not so hard, but on another occasion it might be:
artist=10&location=5
and I'd have to add page=1 onto the end recognising that I also have to add the ampersand.
So basically it could be and of 3 variations:
- taking away from the current query string
- adding to the current querystring
(a) just add the pair page=1 onto the?
(b) add the pair with an & in front: &page=1
(c) add the pair with an & at the end?page=1&
(d) add the pair with an & at front and end &page=1&
- not adding anything
Hope that makes sense.
function BuildQueryString($args)
{
$Query = "";
if (count($args) > 0)
{
$sep = '?';
foreach($args as $arg => $value)
{
$Query .= $sep.$arg."=".$value;
$sep = '&';
}
}
return $Query;
}
The return string of this function can be appended to your link.
On PHP5 also http_build_query [us3.php.net] can be used.
Arjan
Or in the function add an if statement within the foreach loop to check whether the argument is empty, like:
if ($value!== "")
{
$Query .= $sep.$arg."=".$value;
$sep = '&';
}
Use!== and not!= for the comparison.
Also the assigment of the $sep variable should be within the 'if'.
Arjan
PS: Why is a space before a! removed?
Arjan