Passing constantly changing query strings PHP
get, php, query-string, url
Solution
You can store your filters in an associative array :
$myFilters = array(
"popularity" => "low",
"sort" => "newest"
);
Having your filters stored in an associative array ensures you only have 1 value for each of them. Then you can use http_build_query to build your querystring :
$myURL = 'http://www.example.com/test.php?' . http_build_query($myFilters);
This will result in the following URL :
http://www.example.com/test.php?popularity=low&sort=newest
Edit : Oh, and if the filters order in the querystring matters, you can sort the associative array before building your URLs :
asort($myFilters);
$myURL = 'http://www.example.com/test.php?' . http_build_query($myFilters);
Problem
I have a list of articles on my page, and I would like to be able to apply a myriad of sorts and filters to it by appending $_GET values to the URL: http://www.example.com/blogs.php?sort=newest&popularity=high&length=200 If I have links on my page to append these values to the url...they need to be smart enough to account for any previous sorts/filters applied. EXAMPLE 1: if i currently have... http://www.example.com/blogs.php?sort=newest and then i want to attach an additional filter of popularity=high, i need to have this: http://www.example.com/blogs.php?sort=newest&popularity=high and not this: http://www.example.com/blogs.php?popularity=high EXAMPLE 2: if i have... http://www.example.com/blogs.php?popularity=high and i try to alter my popularity filter, i do not want: http://www.example.com/blogs.php?popularity=high&popularity=low so simply tacking on to the query string won't fly. Therefore, what is a scalable way to build my filter links so that they "remember" old filters, but will still overwrite their own filter value when needed?