0

I'm having a problem with some specific PHP code that I've been working on for my food ordering system. It's meant to be a reporting code where it uses number of sold items saved in the database and comes up with a simple table showing the top 10 sold items with the respective names in the second column.

But, as far as i've worked with it, it always gives a mixture of sold item such as, 17, 10, 8, 12, 30 item sold.Here's the code I've been working on. Any ideas or amendments i can try? Thanks in advance!

<?php
   include("includes/db.php");
   if(isset($_SESSION["admin"]))
   {
?>                  
 <br><h2 align="center">Top Sellers' Report</h2>
 <table border="0" cellpadding="0" width="500px" align="center" style="border:2px solid black;">
 <tr>
      <td align="center">Sales Information<br></td>
      <td align="center">Item Name<br></td>
  </tr>
  <tr>
      <td colspan="4"><hr size="2" color="black" /></td>          
  </tr>

<?php
    $result=mysql_query("select DISTINCT menurecords.menuname, orderdetails.menuID 
                         from orderdetails 
                         JOIN menurecords ON orderdetails.menuID = menurecords.menuID 
                         ORDER BY orderdetails.quantity DESC") 
                         or die("select * from menurecords"."<br/><br/>".mysql_error());

    while($row=mysql_fetch_array($result)){
?>

  <tr>
      <td align="center">
<?php
    $menuID=$row[1]; 
    $result2=mysql_query("select SUM(quantity) from orderdetails   WHERE orderdetails.menuID='$menuID'");
    while($row2=mysql_fetch_array($result2)){
?>
        Quantity-Sold: <?php echo $row2[0]?><br>    
<?php   
    }
?>

    </td>
    <td align="center">
        <b><?php echo $row[0]?></b></td>
</tr>
<tr>
    <td colspan="4"><hr size="2" /></td>
<?php 
    }
?>
<?php
} else
    echo "No session exists. Please login again to view this page by clicking <a href='adminlogin.php'>here</a>."
?>  
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Ray Light
  • 15
  • 1
  • 6
  • By the looks of it your getting the top results from the database, adding them together and then outputting them without really sorting if the first result is the highest seller. – Liam Sorsby Sep 23 '13 at 18:06
  • @liamSorsby yes, since when the customer orders an item, the quantity saved is default 1. the code then is required to count the number of 1's in the database for the particular item, and display it from top sold to less sold. – Ray Light Sep 23 '13 at 18:35
  • so at present you are just outputting the results without sorting. try storing the results in an array and then after your loop, use sort($array) to display your results in a sorted list. – Liam Sorsby Sep 24 '13 at 11:25
  • @liamsorsby i'm unsure how the array should look like. any rough ideas? it will be deeply appreciated. – Ray Light Sep 24 '13 at 17:54
  • instaed of echo $row[0] try $data[] = $row[0]; then after your loop try sort($data); this will sort your data in order. Edit: try arsort() instead of sort and this sorts in decending order – Liam Sorsby Sep 25 '13 at 09:12
  • similiar to bubble sort? – Ray Light Sep 25 '13 at 13:44
  • I had to lookup what bubble sort did. Apparently bubble sort is a bad idea and they recommend using the usort or arsort would be the best options. see http://stackoverflow.com/questions/9001294/bubble-sort-in-php – Liam Sorsby Sep 25 '13 at 13:58

0 Answers0