Entry
How do I set the order of the output from a database to the display?
Feb 28th, 2007 02:58
Articles Way, Philip Olson, Scott Sample,
You really should do this in the query itself and not at the PHP level
as the database is more efficient at sorting than PHP.
Assuming SQL you would use the ORDER BY clause in your SQL statement.
For example:
// Sorted as you entered it into the database
SELECT fname, lname, email FROM names
// Sorted by the lname column in ascending (a-z) order
SELECT fname, lname, email FROM names ORDER BY lname
Ascending order is the default, for descending (z-a) order you may do:
// Sorted by the lname column in descending order
SELECT fname, lname, email FROM names ORDER BY lname DESC
Let's say multiple lnames exist, let's sort it by lname first, and
then
fname, like so:
// Sorted by the lname column in ascending (a-z) order
SELECT fname, lname, email FROM names ORDER BY lname,fname
For more information on the ORDER BY clause consider the following
tutorial on the subject:
http://www.sqlcourse2.com/orderby.html
Now it's all sorted so you'd retrieve the data as usual, perhaps like
so:
while ($row = mysql_fetch_assoc($result)) {
echo $row['lname'];
echo $row['fname'];
}
======
kirupa.com/web/mysql_xml_php.htm
i hope that helps.
http://americanahost.com
http://www.qwesz.com