display in sorted order from DB

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
inosent1
Forum Commoner
Posts: 97
Joined: Wed Jan 28, 2009 12:18 pm

display in sorted order from DB

Post by inosent1 »

i want to have the data displayed from the DB sorted in a particular way. right now i call the data from the DB and it displays in the order of the table, and then use a javascript header gizmo to sort.

but i would rather the data show up sorted in the first place and use the js sort when i need to.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: display in sorted order from DB

Post by twinedev »

Code: Select all

$sql = 'SELECT `field1`, `field2` FROM `tblStuff` WHERE `category` = "'.mysql_real_escape_string($strFilter).'" ORDER BY `field3` ';
For decending order:

Code: Select all

$sql = 'SELECT `field1`, `field2` FROM `tblStuff` WHERE `category` = "'.mysql_real_escape_string($strFilter).'" ORDER BY `field3` DESC ';
You can sort on more than one field, separate them by commas. Note the ASC (default if not given, as in first example above) or DESC needs to be with each field:

Code: Select all

ORDER BY `field1` DESC, `field2` ASC
-Greg
inosent1
Forum Commoner
Posts: 97
Joined: Wed Jan 28, 2009 12:18 pm

Re: display in sorted order from DB

Post by inosent1 »

thanks!
Post Reply