Jump to content

Quantity Rating


Dysan

Recommended Posts

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

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

<?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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.