tags:

views:

59

answers:

2

How can I sort this array by city or by id in descending order?

if ($num > 0 ) {
$i=0;
while ($i < $num) {
$city = mysql_result($result,$i,"city");
$state = mysql_result($result,$i,"state");
$id = mysql_result($result,$i,"id");

echo "$city";
echo "$state";

++$i; } } else { echo "No results."; } ?>
+2  A: 

You didn't include your SQL code in the above post, but that is the part of the code where you are supposed to add the ORDER BY sorting function as follows:

SELECT * FROM address_table ORDER BY city desc

Bug Magnet
Thanks, you're right and it worked great for sorting by id in descending order. How would I show only one city though?
Jon
A WHERE clause. E.g. `SELECT * FROM address_table where id = ? ORDER BY city desc`
Matthew Flaschen
I am not sure what you mean... do you want to show all addresses that are from one particular city? (e.g. all addresses that are in the city of Beijing) If this is what you are trying to do, then use something like this:SELECT * FROM address_table where city = 'San Francisco'This will return a full list of addresses that are in the city of San Francisco.Is this what you meant by "one city"?
Bug Magnet
A: 

you can also use "LIMIT" for one city: SELECT * FROM address_table ORDER BY city desc LIMIT 0,1 - returns 1 row

STEVER