Jump to content

unable to add up values in SESSION


jasonc

Recommended Posts

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

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;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.