0

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."; } ?>
3
  • 10
    Put an order by clause in your query? Commented Jun 3, 2010 at 2:45
  • Donnie -- if you made that an answer it'd be the best. Commented Jun 3, 2010 at 2:52
  • Thanks, that worked great for sorting by id in descending order. How would I show only one city though? Commented Jun 3, 2010 at 2:53

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, you're right and it worked great for sorting by id in descending order. How would I show only one city though?
A WHERE clause. E.g. SELECT * FROM address_table where id = ? ORDER BY city desc
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"?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.