Hi
I am having a problem with a session array and hope someone can correct my problem.
When i post a page which allows the user to select the quantity of a product they wish to buy, I run this code:
if (!empty($_POST['quantity'])) {
$_SESSION['cart'][$_POST['product']] = array('quantity' => $_POST['quantity'], 'productprice' => $_POST['productprice']);
$totalordervalue = CalculateMyOrder();
}
And the function which is called CalculateMyOrder is below:
function CalculateMyOrder () {
$quantitytotal = 0;
$pricetotal = 0;
foreach($_SESSION['cart'] as $product) {
$quantitytotal += $product['quantity'];
$pricetotal += $product['quantity'] * $product['productprice'];
$newprice += $pricetotal;
}
$totalordercost = $newprice;
$_SESSION['S_TEMP_BASKET_QUANTITY'] = $quantitytotal;
$_SESSION['S_BASKET_TOTAL_QUANTITY'] = $quantitytotal;
$_SESSION['S_BASKET_TOTAL_PRICE'] = $totalordercost;
return $totalordercost;
}
The problem i am having is that when a user add a 2nd item to the cart, the function above is adding the new item price to the 1st item price plus the first item price again.
And when i add a 3rd item to the basket, it adds all items together plus, the 1st and 2nd items again and so on ..
I hope that is clear and i havent confused you.
How can i prevent the $_SESSION['S_BASKET_TOTAL_PRICE'] from being updated incorrectly?
Thanks in advance.