I figure it would be handy to have a query that would show me the number of times a product had sold in the store and ALSO the profit it resulted in. This code below works however...the prices arent right. Everything is the quantity X $3.49....but not all of my products are 3.49..some are 17.00 etc. Why is it doing this?
<?php
$rowsPerPage = 500;
if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 0;
}
$query = "SELECT sum(a.quantity) AS quan, a.productId, b.name FROM `".$glob['dbprefix']."CubeCart_order_inv` a INNER JOIN `".$glob['dbprefix']."CubeCart_inventory` b ON a.productId = b.productId GROUP BY productId DESC ORDER BY `quan` DESC";
$results = $db->select($query, $rowsPerPage, $page);
$noResults = $db->numrows($query);
$totalItemsSold = $db->select("SELECT SUM(quantity) as totalProducts FROM `".$glob['dbprefix']."CubeCart_order_inv`");
$prodprice = $db->select("SELECT (`price`) as prodprice FROM `".$glob['dbprefix']."CubeCart_inventory`");
if($results==TRUE) {
$i=0;
$chartData = array();
$keyStr = "<table width='100%' border='0' cellspacing='1' cellpadding='3' class='mainTable'>
<tr>
<td class='tdTitle' style='text-align: center;'>Rank</td>
<td class='tdTitle'>Product Name</td>
<td class='tdTitle' style='text-align: center;'>No of Sales</td>
<td class='tdTitle' style='text-align: center;'>Percentage of Sales</td>
</tr>\n";
for ($i=0; $i<$noResults; $i++){
$cellColor = cellColor($i);
$percentage = ($results[$i]['quan'] * $prodprice[0]['prodprice']);
$percentage = number_format($percentage, 2);
if ($percentage >= 0 && $results[$i]['quan'] >= 1) {
$position = (($page*$rowsPerPage)+1)+$i;
$chartData[] = array($position,$percentage);
$keyStr .= "<tr>
<td class='copyText' style='text-align: center;'>".$position."</td>
<td class='copyText'>".$results[$i]['name']."</td>
<td class='copyText' style='text-align: center;'>".$results[$i]['quan']."</td>
<td class='copyText' style='text-align: center;'>$".$percentage."</td>
</tr>\n";
}
}
$keyStr .= "</table>";
?>
<?php
echo $keyStr;
} else {
echo "<p class='copyText'>No Products have sold in your store.</p>";
}
?>