views:

59

answers:

2

In several PHP applications I'm making I need to maintain a very long query string for filters for a table. I only want to change one or more GET variables per filter change, while the rest of the query string stays unchanged.

I have a few ideas on how to do this, including using javascript to build the query string every time a filter is changed, or maintaining the query string in a session variable, changing it from there.

I would like your opinions or experience on how the best way to do this would be.

Thanks!

+2  A: 

If you have all params in GET array, why don't you build a function that makes query string from them?

$params = array('key' => 'val', 'oth' => 'inn');

foreach($params as $key => $param) $string .= '&'.$key.'='.$val;

Tomasz Kowalczyk
+1  A: 

Yes, use an associative array to store a map of keys to values. Then you can change one easily. These are supported well in both PHP and Javascript, so you're good.

You'll just need to write functions that output the array as a query string.

I did this on a lark once (and now I do it every time): alphabetize (or otherwise order) the parameters in the query string. Why? It will be convenient if the same page always has the same URL/query string. If you write tests, it's easier to assert that query strings match. Otherwise, you will be at the mercy of the associative array implementation's traversal order.

ndp