Jon
Jon

Reputation: 93

How do you sort php and sql arrays?

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."; } ?>

Upvotes: 0

Views: 138

Answers (2)

Stepan Suvorov
Stepan Suvorov

Reputation: 26156

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

Upvotes: 1

Bug Magnet
Bug Magnet

Reputation: 2668

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

Upvotes: 4

Related Questions