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. Link to comment https://forums.phpfreaks.com/topic/240394-unable-to-add-up-values-in-session/ 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; } ?> Link to comment https://forums.phpfreaks.com/topic/240394-unable-to-add-up-values-in-session/#findComment-1234847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.