Page 1 of 1
Sorting arrays
Posted: Mon Jun 21, 2004 3:06 am
by lazersam
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.
Posted: Mon Jun 21, 2004 3:22 am
by Wayne
have you tried putting an ORDER BY clause in your SQL statement?
Posted: Mon Jun 21, 2004 5:11 am
by Grim...
If, for some reason, you can't use an ORDER BY: [php_man]array_multisort[/php_man]() ?
Posted: Mon Jun 21, 2004 5:13 am
by Grim...
In case you don't know about ORDER BY:
Code: Select all
SELECT * FROM mytable ORDER BY photo_status DESC
should do what you want it to (change the
mytable section, obviously).
Posted: Mon Jun 21, 2004 4:20 pm
by pickle
Grim... wrote:
Code: Select all
SELECT * FROM mytable ORDER BY photo_status DESC
Since 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.

Posted: Mon Jun 21, 2004 5:48 pm
by Weirdan
pickle 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.

http://dev.mysql.com/doc/mysql/en/SELECT.html
MySQL manual wrote:
.....
The default is ascending order; this can be specified explicitly using the ASC keyword.
.....
Posted: Mon Jun 21, 2004 6:17 pm
by tim
lol i dont like it when your feeling
anal pickle

Posted: Tue Jun 22, 2004 2:11 am
by lazersam
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.
Posted: Tue Jun 22, 2004 9:39 am
by pickle
Weirdan wrote:
MySQL manual wrote:
.....
The default is ascending order; this can be specified explicitly using the ASC keyword.
.....
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.