DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

Associative arrays to update Cart Cookie

By: | 24 Jul 2009 11:50 am

Hi,

Below is a function I have written to update the Cookie of a Shopping Cart.

To me, the logic seems fine But how are Cookies updated from stored Arrays like this ?

This is the original cookie ...
Array ( [ECAcart] => Array ( [coppercasserole] => Array ( [qty] => 1 [desc] =>

Copper Casserole [price] => 2199 ) [steelcasserole] => Array ( [qty] => 1 [desc]
=> Steel Casserole [price] => 2099 ) [steelfrying] => Array ( [qty] => 1 [desc]
=> Steel Frying [price] => 899 ) ) )

The cookie after updating with 1item that already exists in the Cart ...
Array ( [coppercasserole] => Array ( [coppercasserole] => Array ( [qty] => 1
[desc] => Copper Casserole [price] => 2199 ) [steelcasserole] => Array ( [qty]
=> 1 [desc] => Steel Casserole [price] => 2099 ) [steelfrying] => Array ( [qty]
=> 1 [desc] => Steel Frying [price] => 899 ) ) [steelcasserole] => Array ( [qty]
=> 1 [desc] => Steel Casserole [price] => 2099 ) [steelfrying] => Array ( [qty]
=> 1 [desc] => Steel Frying [price] => 899 ) )

=========== ================ =============== ============ =============

<?php

$cartArray = 0;
$cItem = $_POST[`item`]; // <<< Key without single quotes
$cartItem = "`" . $_POST[`item`] . "`"; // <<< Key with single
quotes
$cartDesc = fConvertToDescription($_POST[`item`]);
$cartPrice = fGetPanPrice($_POST[`item`]); // <<< Extracts price of item
from database.
$cartQty = 0;
if ( !(isset($_COOKIE[`ECAcart`])) ) {
echo "********* No shopping cart - ******** <br /><br />";
$cartArray = array();
$cartArray[$cartItem] =
array(`qty`=>1,`desc`=>$cartDesc,`price`=>$cartPrice);
$cartString = serialize(addslashes($cartArray));
setcookie(`ECAcart`,$cartString,time()+60*60*6);

return $cartArray;
};

$cartArray = unserialize(stripslashes($_COOKIE[`ECAcart`]));

if ( array_key_exists($cItem,$cartArray) ) {
echo "********* Key EXISTS - " . $cItem . " ******** <br /><br />";
$cartQty = $cartArray[$cItem][`qty`] + 1;
$cartArray[$cItem][`qty`] = $cartQty;
$_COOKIE[`ECAcart`][$cartItem][`qty`] = $cartQty;

}else{

echo "********* Key does NOT exist - " . $cartItem . " ******* <br
/><br />";
$cartArray[$cartItem] =
array(`qty`=>1,`desc`=>$cartDesc,`price`=>$cartPrice);
$cartString = serialize(addslashes($cartArray));
setcookie(`ECAcart`,$cartString,time()+60*60*6);

};
return $cartArray;

};

?>

Comments

No Comments Posted for this Article.

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move