Passing data to the order by clause?

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
ownG
Forum Newbie
Posts: 1
Joined: Wed Dec 18, 2002 3:36 am

Passing data to the order by clause?

Post 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?
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

Post 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
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post 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
Post Reply