cant get grand total of a shopping cart PHP/MySQL

mannix

New Member
Im trying to grab the grand total of my shopping cart but i just cant seem to get it to work and im wondering if its the way i have approched my code, I have used a foreach loop to grab the id from a session array and then loop through the results however when i try to do a SUM query the answer just outputs the individual prices as an array as apposed to adding them together. Would it be better to store the price in a 2d array ?\[code\]if(!isset($_SESSION['cart'])){//$_SESSION['cart']=array();//creating session array to store items id} <?php echo'<table class="table">'; echo'<tr>'; echo'<th>Item</th>'; echo'<th>Code</th>'; echo'<th>Description</th>'; echo'<th>Cost(GBP)</th>'; echo'<th>Remove</th>'; echo'</tr>'; foreach ($_SESSION['cart'] as $value){ $z = mysql_query('SELECT * FROM product_item where id="'.$value.'"'); while($row = mysql_fetch_array($z)){ echo'<tr>'; echo'<td><img src="http://stackoverflow.com/questions/9307787/images/'.$row['path'].'.jpg" alt="'.$row['alt'].'" width="65" height="65"/></td>'; echo'<td>'.$row['code'].'</td>'; echo'<td>'.$row['description'].'</td>'; echo'<td><p>&pound;'.$row['price'].'</p></td>'; echo'<td><p><a title="remove from shopping cart" href="http://stackoverflow.com/questions/9307787/cart.php?remove='.$value.'">X</a></p></td>'; echo'</tr>'; } // this is where i want to get total cost $y = mysql_query('SELECT SUM(price) as total_cost FROM product_item where id="'.$value.'"'); while($row = mysql_fetch_array($y)){ echo $row['total_cost']; } } echo'<tr>'; echo'<td></td>'; echo'<td></td>'; echo'<td></td>'; echo'<td><p>Total</p></td>'; echo'<td></td>'; echo'</tr>'; echo'</table>'; } ?> ?>\[/code\]
 
Top