[SOLVED] Sorting arrays

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
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Sorting arrays

Post 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.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

have you tried putting an ORDER BY clause in your SQL statement?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

If, for some reason, you can't use an ORDER BY: [php_man]array_multisort[/php_man]() ?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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. :wink:
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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. :wink:
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.
.....
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

lol i dont like it when your feeling anal pickle

8)
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply