Jump to content

Archived

This topic is now archived and is closed to further replies.

barkster

Sorting Search Result with $_GET

Recommended Posts

I'm trying to implement a sorting on my recordset using  $_GET variables.  What is the best way to implement this?  I want to have a link on a page for ordering by price and date but do I have to capture the url string and replace the order variable somehow to create the link with the correct sorting.  I want them to be able to sort back and forth between price and date but I can't figure out how to change the url parameters on the page.  for instance SearchResults.php?Keyword="Test"&Description=""&Sorting="Price"&Order="Desc"

Thanks

Share this post


Link to post
Share on other sites
Are you grabbing the data from a MySQL table???

If so

SELECT * FROM table WHERE field LIKE '%".$keyword."%' ORDER BY ".$Sorting." ".$Order."

So it looks something like
SELECT * FROM table WHERE fieldname LIKE '%test%' ORDER BY Price DESC

Just please remember to do some checks before using the GET vars, or you will be open to SQL INjection

Share this post


Link to post
Share on other sites
Yeah know how to do that part and I probably was't that clear.  My problem is creating the link for instance if someone does a search and I'm doing $_Get to get the search keywords etc... then I have to incorporate that back into the link on the page for the sorting.  For instance if searchresults.php?keyword=test&description="" then I need to create a link on the page to searchresults.php?keyword=test&description=""&sorting=price&order=asc but then I want to have them be able to sort by date also so then I would have to get the url and change the sorting to sorting=date  I guess I'm answering my own question but I didn't know if I had to get the url string and strip the sorting=date out and replace with sorting=price or if there was an easier way of building those dyamic links somehow

Share this post


Link to post
Share on other sites
<a href='".$_SERVER['PHP_SELF']."?Keyword=".$_GET["Keyword"]."Description='".$_GET["Description"]."Sorting=Something&Order=Desc'>

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.