tags:

views:

49

answers:

3

Is there a good way to keep consistency in the $_GET

For example if i have a url http://www.example.com/index.php?Id=5&sort=new

And i want to now add a new $_GET field at the end of that so is there any way i can create a function that will keep the Id and sort and add my new var lim?

So the URL Now looks like: http://www.example.com/index.php?Id=5&sort=new&lim=100

maybe something like:

add_new_get('http://www.example.com', 'lim=100');

Im sure i can come up with a function but just wondering if someone has implemented such thing that they would like to share :D

+2  A: 

you could always just do $_SERVER['REQUEST_URI'].'&yourstuff=whateves'; I guess you could wrap that in a function if you really wanted to.

the above includes everything before the file name. if you want ONLY the get requests you can do:

$new_req = "?";
foreach ($_GET as $key=>$val){
    $new_req .= "&".$key."=".$val;
}
$new_req .= $your_new_value;
contagious
You are correct, But hey another favour :D what if the url is like: http://www.example.com/xyz/index.php, The above works such as: xyz/index.php
Shahmir Javaid
see the edit above
contagious
+2  A: 

You might find it preferable to use a class to manage your query string handling rather than writing your own.

See HTTPQueryString

It isn't terribly complex to write a function to take a url and build your query string, but with a class like HTTPQueryString, you don't have to worry about making sure you have the right encoding, tests for whether there are already variables present in the string, etc.

Edit

e.g.

$query = new HttpQueryString(false, 'Id=5&sort=new');
$query->mod('lim=100');
echo $query->toString(); // echoes http://www.example.com/index.php?Id=5&sort=new&lim=100
Jonathan Fingland
Ty for the solution, im liking this HTTPQueryString :D
Shahmir Javaid
it's a fairly robust solution. As a bonus, used as a singleton, all classes relying on it can work on the same query string, adding their own parameters as necessary and never needing to worry about what other classes do with it. Obviously, you'd need to take steps to avoid collisions, though.
Jonathan Fingland
as an addendum to my last comment, you might want to write a wrapper class for it anyways, just in case you might later want change how your GET variable management is handled
Jonathan Fingland
A: 

A slightly more hackish solution is to use output_add_rewrite_var and output_rewrite_vars

Kristoffer S Hansen