I think this might be quite basic but I am still trying to learn and not entirely sure how to do this!
I want to create a sum of all $qty values from the script below and then display this sum at the bottom. What is the best way of doing this?
Code: Select all
function printCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="basket.php?action=update" method="post" id="cart">';
$output[] = '<table width="600" align="center">';
$output[] = '<tr><th>Item</th><th>Item Price</th><th>Quantity</th><th>Subtotal</th></tr>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM products WHERE Product_ID = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td align="center">'.$Common_Name.' ('.$Genus.')</td>';
$output[] = '<td>£'.$Price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>£'.(($Price) * $qty).'</td>';
$total += (($Price) * $qty);
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<h2 align="right">Total: £'.$total.'</h2>';
$output[] = '</form>';
}
return join('',$output);
}