PHP URL GET parameters, if exist, replace?
You can use the $_GET superglobal to get each individual get variable. If you put that in an array, you can overwrite any value by just setting it again:
$params = $_GET;
$params['sd'] = "whateveryoulike";
$paramString = http_build_query($params);
Matt
Updated on July 29, 2022Comments
-
Matt almost 2 years
Trying to do an advanced search options, with sorting of different options ASC or DESC.
Example URL:
search.php?accom_type=x&no_rooms=x&rooms_total=x&prop_area=x&rent_less=&rent_more=&available=&go=Search
&sd=a
Highlighted bold is &sd (sort direction) option. The previous variables are passed through a form which is filled in.
Now I have links like this..
<a href="<?=$_SERVER['REQUEST_URI']?>&sd=a">ASC</a>|<a href="<?=$_SERVER['REQUEST_URI']?>&sd=d">DESC</a>
Which is obviously wrong because I'm using
REQUEST_URI
- because if a person changes after it is initially set the URL will be:&sd=a&sd=d
I'm sure I have come across this problem before, but can't quite think how I solved it.
How do I check if a GET is already set (such as sd) and if so, change it, otherwise, add it to the end of the URL, to produce the links shown above.
Edit: Maybe a screen shot would help to understand: http://dl.dropbox.com/u/10591127/Capture.PNG
Cheers, Matt
-
Matt about 13 yearsGreat thank you, but I'm not entirely sure how to execute this - would it not need to be repeated for each link? I have many links like this... <a href="<?=$_SERVER['REQUEST_URI']?>&sort=r">Rent</a> | <a href="<?=$_SERVER['REQUEST_URI']?>&sort=av">Availabity</a>