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; } } Link to comment https://forums.phpfreaks.com/topic/77507-quantity-rating/ 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++; } ?> Link to comment https://forums.phpfreaks.com/topic/77507-quantity-rating/#findComment-392325 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>'; } ?> Link to comment https://forums.phpfreaks.com/topic/77507-quantity-rating/#findComment-392333 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? Link to comment https://forums.phpfreaks.com/topic/77507-quantity-rating/#findComment-392336 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 Link to comment https://forums.phpfreaks.com/topic/77507-quantity-rating/#findComment-392373 Share on other sites More sharing options...
pocobueno1388 Posted November 15, 2007 Share Posted November 15, 2007 Ah, I see. Thanks =] Link to comment https://forums.phpfreaks.com/topic/77507-quantity-rating/#findComment-392380 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.