开发者

PHP URL GET parameters, if exist, replace?

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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜