Jump to content

Archived

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

corvyr

Sorting help

Recommended Posts

I'm pretty new to PHP and I'm wondering how I would sort a table using 2 different fields, for example sorting by age and then by name.

Share this post


Link to post
Share on other sites
[!--quoteo(post=352105:date=Mar 6 2006, 09:09 AM:name=Aaron Korr)--][div class=\'quotetop\']QUOTE(Aaron Korr @ Mar 6 2006, 09:09 AM) [snapback]352105[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I'm pretty new to PHP and I'm wondering how I would sort a table using 2 different fields, for example sorting by age and then by name.
[/quote]

Sorting tables works best through SQL statements if you're using a database.

$query = "SELECT * FROM users ORDER BY name ASC, age DESC";
$qresult = mysql_query($query);

while($row = mysql_fetch_array($qresult))
{
qarray[] = $row;
}

...qarray will then be an array filled with your sorted values from the database. The SQL statement sorts first by name in ascending (ASC) order...or A-Z...and then by age in descending (DESC) order...or oldest to youngest.

Share this post


Link to post
Share on other sites

×

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.