Forum Moderators: coopster

Message Too Old, No Replies

change querystring dynamically

         

jackvull

9:46 am on Jun 14, 2006 (gmt 0)

10+ Year Member



The querystring on my pages change constatly depending on what I want to display so I have to put in this string into the other links on the page dynamically.

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?

eelixduppy

10:56 am on Jun 14, 2006 (gmt 0)



You can use str_replace [us3.php.net] if I understand you correctly, but you have to know what you are looking to replace.

jackvull

11:16 am on Jun 14, 2006 (gmt 0)

10+ Year Member



That's the problem, I won't always know because I kight have a string like:
page=1&artist=10

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.

henry0

11:42 am on Jun 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you instead pre-set the four stings
a.b.c.d
Do an if something ....
and use eelixduppy suggestion to use str_replace()

adb64

11:50 am on Jun 14, 2006 (gmt 0)

10+ Year Member



What I do in situations like this is first split the query string into an array in which each elements key is the arguments name and the elements value the value of the argument. I use parse_str [us3.php.net] to do this. Then add/remove elements to/from this array and turn it back into a query string using the following function:

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

mikesmith76

4:48 pm on Jun 14, 2006 (gmt 0)

10+ Year Member



I apologise if this is the dumb suggestion of the group but can't you just make the changes you want and rebuild the query string from the $_GET array?

Or am i missing the point entirely?

jackvull

9:55 am on Jun 15, 2006 (gmt 0)

10+ Year Member



I think you could use $_GET but as I won't always know whether the querystring has 2 variables or 10, there would be a lot of isset($_GET... checks.

I think the way above is good as I can change the one variable I know is there and just append the rest.

jackvull

10:07 am on Jun 15, 2006 (gmt 0)

10+ Year Member



I appear to get 2? in the quesrystring now above.
Is that because PHP's $_SERVER['QUERY_STRING'] already has a? in it

jackvull

10:09 am on Jun 15, 2006 (gmt 0)

10+ Year Member



forget that last comment. My code had the extra? in it and then the function added an extra one...as it should.

Thanks for the function.

jackvull

10:52 am on Jun 15, 2006 (gmt 0)

10+ Year Member



How do you completely remove one of the arguments from the querystrnig.
For example,?a=1&b=2

I want to remove a=1. I can't just set $args['a'] = "";
because isset checks will pick it up?

adb64

12:06 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



Use unset [us3.php.net]. In your example it would become unset($args['a']);

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?

jackvull

1:18 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



Isn't it always!= as of PHP4.

I've tried!== on some other scripts and it doesn't always work?

adb64

1:50 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



You have to use the!== operator, because when you use!= "" any argument having a value of 0 would also not be included in your new query string.
It is still supported [nl3.php.net].

Arjan