Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to add/change 1 GET value while keeping others?

Tags:

php

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

like image 931
JD Isaacks Avatar asked Jun 03 '10 14:06

JD Isaacks


People also ask

Does the data type of a variable change when the value inside the variable changes?

Short answer: no.

How do I change the value of a forEach?

The following code will change the values you desire: var arr = ["one","two","three"]; arr. forEach(function(part, index) { arr[index] = "four"; }); alert(arr);

How do you change a value in an array?

To change the value of all elements in an array: Use the forEach() method to iterate over the array. The method takes a function that gets invoked with the array element, its index and the array itself. Use the index of the current iteration to change the corresponding array element.


2 Answers

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.

like image 94
Simon Avatar answered Nov 20 '22 23:11

Simon


http_build_query

like image 37
Your Common Sense Avatar answered Nov 21 '22 01:11

Your Common Sense