8

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

2
  • An if statement should works perfectly. Use isset() to check if a variable is set. Commented Feb 5, 2011 at 18:23
  • Would not solve the problem to replace the existing _GET Commented Feb 5, 2011 at 18:30

1 Answer 1

19

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Great 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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.