Jump to content

Sorting Search Result with $_GET


barkster

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
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.