I have Problem Writing a Script to to combine similar product and then add Quantities;
I Would like to display my best selling item;
I want to display the result and keep the database intact;
Thank for any help
Example
Product Name | Quantities Sold
Grape | 1
Banana | 5
Mango | 1
Banana | 3
Grape | 1
Peach 4
Peanut | 2
Apple | 5
Wine | 6
Peach | 1
Apple | 2
Product Name | Quantities Sold
Banana | 8
Apple | 7
Wine | 6
Peach | 5
Peanut | 2
Grape | 2
Mango | 1
I am definitely doing something wrong, But This is what i have so far:
$sql3 = "SELECT name, qty
FROM order_item";
$result2 =& dbQuery($sql3) or die('Cannot get Product. ' . mysql_error());
Print "<table border cellpadding=3>";
while($row2 =& dbFetchAssoc($result2)) {
$product[$i]['name']=$row2['name'];
$product[$i]['qty']=$row2['qty'];
for ($num=0; $num <= count($product); $num++ )
{
if($product[$num]['name']!=$row2['name'])
{
$product2[$num]['qty']+=$row2['qty'];
break;
}
}
Print "<tr>";
Print "<th>Name:</th> <td>".$product[$i]['name']. . "</td> ";
Print "<th>Qty:</th> <td>".$product2[$i]['qty']. " </td></tr>";
$i++;
}
Print "</table>";
Result
Name: Apple Qty: 1
Name: Peach Qty: 1
Name: Wine Qty: 1
Name: Peach Qty:
Name: Apple Qty:
Name: Pear Qty: 1
Name: Banana Qty: 2
Name: Grape Qty: 5
Name: Peanut Qty: 6
Name: Beer Qty: 1
Name: Grape Qty: