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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.