adding get variables to url?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

adding get variables to url?

Post by fael097 »

hi, i have a question, that i didnt manage to figure out myself.
you know when you have a search engine, and you want to apply filters, and sort stuff by given parameter?
if you simply make this:

Code: Select all

<a href="?filter1=low">low prices</a>
<a href="?filter2=best">best rated</a>
it wont keep both filters, if you click the first one, and then click the second one, it will replace the first clicked.
my workaround for this so far:

Code: Select all

<a href="?filter1=low&filter2=<?php echo $_GET['filter2']; ?>">low prices</a>
<a href="?filter2=best&filter1=<?php echo $_GET['filter1']; ?>">best rated</a>
but thats not good when you have like 10 filters (wich is my case) you'll have an extense code for each anchor href...
so i was trying to figure out a way to simply add variables to a $url variable, like if $_GET[variable] is set, $url.="add variable", and if that variable is already set, replace if its different, or keep if its equal, but i dont know if thats possible cuz i didnt manage to make it.
is it doable?
thanks in advance!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: adding get variables to url?

Post by AbraCadaver »

Depends on how long you want to keep the filters. Just from the previous page, or maybe from several pages before? For persistent filters I would probably put these in a session var, then just use the filters from the session.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
fael097
Forum Commoner
Posts: 34
Joined: Sat Mar 06, 2010 7:57 pm

Re: adding get variables to url?

Post by fael097 »

yeah, sessions is probably the best way to do that, plus i can include a clear filters button :)
Post Reply