tags:

views:

56

answers:

2

How can I make a link that justs adds or changes 1 GET var while maintaining all others?

I have a page that is created using different GET vars.

So it will be like mypage.php?color=red&size=7&brand=some%20brand

So I want to have a link that sets it to page=2 or size=8. Whats the easiest way to have a link do that without reseting all the other vars?

I hope that makes sense, let me know if I need to further explain anything

+3  A: 

http_build_query

Col. Shrapnel
also, parse_str. Parse the querystring into an array with parse_star, alter only the keys you deem necessary, and then feed the array to http_build_query. I've used this technique with success.
Frank Farmer
@Frank time to learn $_GET array, pal :)
Col. Shrapnel
@Shrapnel: sometimes you want to manipulate a URL other than that of the currently requested page. e.g., I used the above technique to add google analytics tracking tags to every URL in an html email.
Frank Farmer
+3  A: 

You can parse the url with parse_str to get the values of the url. You can then build a http query by using http_build_query:

$query_arr = $_GET; //or parse_str($_SERVER['QUERY_STRING'], $query_arr)
$query_arr["page"] = 2;
$query_arr["size"] = 8;

$query = http_build_query($query_arr);

EDIT: Sorry I mixed up the two functions ... its parse_str() of course.

Simon
Hi this "seems" like exactly what I need. However I don't think parse_query is a built in PHP function. I tried replacing it with parse_url but that just gives me an array of the different parts of the url (e.g. scheme, host, path, etc.)
John Isaacks
OK I got it to work using your code but changing lines 1 and 2 to just $query_arr = $_GET
John Isaacks
Yeah... sorry, see edit
Simon