[SOLVED] Sorting arrays
Moderator: General Moderators
Sorting arrays
Hi all
This may seem a newbie question but I just cant figure it out! I am getting results from a database with "$row = mysql_fetch_array($qry);" and I want to sort the results.
One of the keys is called photo_status and contains an interger. I need to be able to sort the results with the higher value data in photo_status at the top of the results array.
I have played with "asort" and the like but nothing seems to work. My books are unhelpful cos the examples given cause errors when I run them.
How would you do it?
Thanks
Larry.
This may seem a newbie question but I just cant figure it out! I am getting results from a database with "$row = mysql_fetch_array($qry);" and I want to sort the results.
One of the keys is called photo_status and contains an interger. I need to be able to sort the results with the higher value data in photo_status at the top of the results array.
I have played with "asort" and the like but nothing seems to work. My books are unhelpful cos the examples given cause errors when I run them.
How would you do it?
Thanks
Larry.
In case you don't know about ORDER BY: should do what you want it to (change the mytable section, obviously).
Code: Select all
SELECT * FROM mytable ORDER BY photo_status DESCSince I'm feeling especially anal today, I'll mention that ordering is done in descending order by default, so explicitely stating DESC isn't really necessary.Grim... wrote:Code: Select all
SELECT * FROM mytable ORDER BY photo_status DESC
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
http://dev.mysql.com/doc/mysql/en/SELECT.htmlpickle wrote:I'm feeling especially anal today, I'll mention that ordering is done in descending order by default, so explicitely stating DESC isn't really necessary.
MySQL manual wrote: .....
The default is ascending order; this can be specified explicitly using the ASC keyword.
.....
Thanks guys. I was hoping someone would know how to sort the array after it had been extracted from the database. Another part of my script has already retrieved selected data in a previous search and stored the results in $row.
$row contains all the records I need to display and I was wanted to offer users the option of reshuffling the results e.g. in order of photo_stat.
$row contains all the records I need to display and I was wanted to offer users the option of reshuffling the results e.g. in order of photo_stat.
DOH!!!! I had a feeling I was wrong when I posted that - but only because the fates new I needed to be made a fool of.Weirdan wrote:MySQL manual wrote: .....
The default is ascending order; this can be specified explicitly using the ASC keyword.
.....
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.