Jump to content


Photo

Help summing specific values from a Cookie array


  • Please log in to reply
2 replies to this topic

#1 MrL8Knight

MrL8Knight
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 08 March 2006 - 04:08 AM

I am building a simple shopping cart and I am having problems trying to add the costs of the items to generate a total cost. I am new at this so forgive me if my technical verbiage isn’t the greatest!

I am trying to sum specific values in my arrays (or the price portions of the arrays). I have tried every imaginable way with the array_sum command and nothing has worked for me!


Here is what my cookie information looks like after I put 2 test items into the cart that cost 18.00 each.


Array ( [0] => 21 [1] => test1 [2] => 12 [3] => 12 [4] => 1 [5] => Please select [6] => Please select [7] => Please select [8] => [9] => 18.00 ) Array ( [0] => 22 [1] => test2 [2] => 12 [3] => 12 [4] => 1 [5] => Please select [6] => Please select [7] => Please select [8] => [9] => 18.00 )

Arrays 21 and 22 both contain items with a price of 18.00
I am trying to add both of the [9] fields (18.00) to come up with a total cost


Here is the code I am using to try to add the price of both items:


<?

foreach ($_COOKIE[mycookie] as $valueq) {

$value3 = unserialize(stripslashes($valueq));
}
echo "Total Cost = " . array_sum($value3) . "\n";

?>

When I load this page, it comes up with a total of 65 instead of 36 (18+18)

I’m thinking the problem is that I am summing all the values when I just need to sum the values of the [9] field which contains the prices.

Any help would be greatly appreciated!!!!!!


MrL8Knight


#2 logu

logu
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 08 March 2006 - 06:34 AM


Hi,
try this code.....
$total =0;
$temp = $_COOKIE[mycookie] ;
for($i=0;$i<count($temp);$i++){
$total += $temp[$i][9];
}



#3 MrL8Knight

MrL8Knight
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 08 March 2006 - 10:29 AM

logu

Thank you for your post, much respect, I tried the script and it gave me a sum of 0 instead of 36. I have posted your code for you to see how I had implemented it, maybey I was missing something.

<?

$total =0;
$temp = $_COOKIE[mycookie] ;
for($i=0;$i<count($temp);$i++){
$total += $temp[$i][9];
}

echo "Total Cost = $total";

?>

BUT WAIT!!!!!I just got my hands on the script to make it work properly from the Guru Banfa
Here it is

<?

$total = 0;

foreach ($_COOKIE[mycookie] as $valueq)
{
$value3 = unserialize(stripslashes($valueq));
$total += $value3[9];
}

echo "Total Cost = " . $total . "\n";

?>


Thanks again, back in the running!!!

MrL8Knight




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users