Jump to content

Recommended Posts

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
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";

?>

[b]BUT WAIT!!!!![/b]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
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.