jasonc Posted June 25, 2011 Share Posted June 25, 2011 foreach($_SESSION['cart'] as $id => $value) { $cartCount += $value; // echo("<br />79:cartCount=".$cartCount."<br />"); $getCost = mysql_query("SELECT `Price` FROM `products` WHERE `Pid` = '" . $id . "' LIMIT 1"); $itemCost = mysql_fetch_assoc($getCost); $cartCost += ($itemCost['Price'] * $value); } the array is like this Array ( [cart] => Array ( [3552] => 2 [quantity] => 1 [amount] => 7.99 [item_number] => 3552 [no_shipping] => 1 [3403] => 2 ) ) what i would like is for this script to add up all the figures for the 'amount' and put the total in $cartCost and the $cartCount to have the total number of items. can anyone see what is wrong with this code as advise what i change to get the result i would like. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 26, 2011 Share Posted June 26, 2011 if the array has all those values, why are you doing a query in the middle of the loop to find out the price? try something as simple as this: <?php $cartCount = 0; $cartCost = 0; foreach($_SESSION['cart'] as $id => $value) { if($id=='quantity') $cartCount += $value; if($id=='amount') $cartCost += $value; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.