Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP URL GET parameters, if exist, replace?

Tags:

url

php

get

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

like image 457
Matt Avatar asked Feb 05 '11 18:02

Matt


1 Answers

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);
like image 102
GolezTrol Avatar answered Oct 05 '22 13:10

GolezTrol