PHP URL GET parameters, if any, replace?

Trying to perform advanced search options with sorting of various ASC or DESC options.

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 in bold is & sd (sort direction). Previous variables are passed through the completed form.

Now I have links like this ..

 <a href="<?=$_SERVER['REQUEST_URI']?>&sd=a">ASC</a>|<a href="<?=$_SERVER['REQUEST_URI']?>&sd=d">DESC</a> 

This is obviously wrong because I use REQUEST_URI - because if a person changes after he was originally set, the URL will look like this:

 &sd=a&sd=d 

I am sure that I have come across this problem before, but I canโ€™t understand how I solved it.

How to check if GET (for example, sd) is installed, and if so, change it, otherwise add it to the end of the URL to create the links shown above.

Edit: Perhaps a screenshot will help to understand: http://dl.dropbox.com/u/10591127/Capture.PNG

Cheers, Matt

+8
url php get
source share
1 answer

You can use the superglobal sum of $ _GET to get each individual variable. If you put this in an array, you can overwrite any value by simply setting it again:

 $params = $_GET; $params['sd'] = "whateveryoulike"; $paramString = http_build_query($params); 
+20
source share

All Articles