Jump to content

trying to sort a select statement


kts

Recommended Posts

Hey everyone, thanks for reading.

 

I have done this before but many moons ago. I am trying to have a few drops downs that will change the Order By clause on a Select statement. From my memory I remember you need to have the order by in a variable with a case statement to control which one it will become. I am having some trouble doing so if anyone has some sample code that they use to do this I would greatly appreciate it. I'm not sure as to why I am not able to re-create it right now, but I do not use case statements very much.

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/146447-trying-to-sort-a-select-statement/
Share on other sites

How about this:

 

  // set the allowed sort fields
  $allowed_sorts = array("name", "date", "amount");
  // use the specified sort, or a default
  $sort = ($_GET['sort'], $allowed_sorts) ? $_GET['sort'] : 'name';
  // now execute the query and continue
  $rs = mysql_query("SELECT * FROM table ORDER BY $sort ASC;");

Archived

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

×
×
  • 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.