Page 1 of 1

Passing data to the order by clause?

Posted: Wed Dec 18, 2002 3:36 am
by ownG
You know the order by clause of the select statement? Is it possible to set up a way where you could click on a link and pass .php?Sortby=ColumName and then setup the select to order by Sortby. I've been messing with that, but couldn't get it to work.

The best I can come up with is just using the same code over and over in php files with differant names to sort the table by differant categories. I know there has to be a way to do it the other way :/

When I tried it the other way, I have it setup like this:

<a href="www.server.com/file.php?sortby=Date">

"select ............... from Table order by sortby desc");

Anyone have the solution?

Posted: Wed Dec 18, 2002 3:59 am
by 9902468
Well, you could do drop box that contains the options

Code: Select all

print('<select name=''sort_by''>
<option value=''name''> Name </option>
<option value=''date''> Date </option></select');
Then just send it to your page using form, js or whatever method that pleases you

then you just say

Code: Select all

$query = 'select column1, column2, from some_table order by '.$_GET&#1111;'sort_by'].' desc';

Or if you send via POST, then ofcourse $_POST['sort_by']....
Hope that helps..

-9902468

Posted: Wed Dec 18, 2002 8:32 am
by f1nutter
Don't forget to take into account the possibility that nothing could be selected, what do you do then?

There is also a post about injection attacks which might be an issue if you allow other parameters to be entered. Just so your aware.

See viewtopic.php?t=4814&highlight=injection+attack