Dysan Posted November 15, 2007 Share Posted November 15, 2007 I have a field in the database called "Quantity". How do I find the 10 most highest numbers, then display them on a rating of 1-10. 1being the highest, and 10 being the lowest. The following code creates the numbers 1-10. for ($i = '1'; ; $i++) { echo $i . '<br>'; if ($i == "10") { echo '<br><br>'; break; } } Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 15, 2007 Share Posted November 15, 2007 Try this <?php $query = "SELECT number, MAX(number) AS max FROM Quantity ORDER BY max DESC LIMIT 10"; $result = mysql_query($query)or die(mysql_error()); $num = 0; while ($row = mysql_fetch_assoc($result)){ echo "$num - {$row['number']}<br>"; $num++; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 15, 2007 Share Posted November 15, 2007 <?php $query = "SELECT quantity FROM tablename ORDER BY quantity DESC LIMIT 10"; $result = mysql_query($query) or die(mysql_error()); $num = 0; while ($row = mysql_fetch_assoc($result)){ echo ++$num, ' - ', $row['quantity'], '<br>'; } ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 15, 2007 Share Posted November 15, 2007 Woah, talk about a brain fart with the query on my part. Barand - Is there any advantage/difference to doing concatenation with commas rather than periods? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 15, 2007 Share Posted November 15, 2007 I just ran a few timing tests and, surprisingly, concatenating was faster then using commas. I would've expected concatenation to involve some memory reallocation and shuffling about and would therefore be slower. Or maybe my test strings were too short. It certainly slows down when strings become several thousand characters long. See http://www.php-editors.com/contest/1/49-read.html Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 15, 2007 Share Posted November 15, 2007 Ah, I see. Thanks =] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.