Connecting Tech Pros Worldwide Forums | Help | Site Map

Summing specific values in a cookie array (newbie)

Newbie
 
Join Date: Mar 2006
Posts: 5
#1: Mar 8 '06
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

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,200
#2: Mar 8 '06

re: Summing specific values in a cookie array (newbie)


You are not using array_sum correctly (in fact it does not apply to the job you are trying to do).

array_sum sums the values in an array, however what you want to do is sum the values in a given index of several different arrays.

What documentation are you using as a reference?

This is the sort of code you want

[php]
<?

$total = 0;

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

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

?>
[/php]


BTW if in $_COOKIE[mycookie] mycookie is a string array index then it is good form to put quotes round it like so $_COOKIE['mycookie']. This protects the array access if case mycookie is accidentally defined as so

[php]

$_COOKIE[mycookie] = 5;

define ( 'mycookie', 2 )

echo $_COOKIE[mycookie]; /* Statement 1*/

echo $_COOKIE['mycookie']; /* Statement 2*/
[/php]

In this code example statement 1 will give an undefined variable warning because it actually trys to access $_COOKIE[2], statement 2 will work correctly display the value 5.
Newbie
 
Join Date: Mar 2006
Posts: 5
#3: Mar 8 '06

re: Summing specific values in a cookie array (newbie)


The script worked perfect! I am now trying to understand how it all comes together and then on to the next phase. Thanks for the tip on the quotes, I was wondering what the best method was since they both worked. Appreciate it!

Thank's Banfa, you are the man. Much respect


Mr. L8Knight
Reply