Hi,
I'm trying to modify a shopping cart script from Mastering PHP/MySQL and am
having trouble setting up some arrays for it. The original code, below,
stores the cart items in a session variable array
($HTTP_SESSION_ VARS['cart'] = array();) basically this stores an associative
array with the id number of the product and the qty (ie cart[$productid] =
qty of that product).
This works fine, but I need to be able to have sub options AND colours for
some (but not all) products. I don't know exactly what to do here, I know I
need some kind of array using a combination of the productid,colou r and
option as the key for it. then storing the quantity of the item in that (and
if possible price).
Here is the code that sets up the session variables and updates the quantity
if an item is already in there (the array I'm trying to modify is the 'cart'
one, $new is the productid of the new item to add (and
$newcolour,$new options will be the colour and option for the new item)).
if($new) { //see if a product has been specified
if(!isset($HTTP _SESSION_VARS['cart'])) { //see if session variables
have been set
$HTTP_SESSION_V ARS['cart'] = array(); //if not create them
$HTTP_SESSION_V ARS['items'] = 0;
$HTTP_SESSION_V ARS['total_price'] ='0.00';
}
if(isset($HTTP_ SESSION_VARS['cart'][$new]{ //check if there is already
a value in cart array for that product
$HTTP_SESSION_V ARS['cart'][$new]++; //if so increase quantity
}
else {
$HTTP_SESSION_V ARS['cart'][$new] = 1; //otherwise just put one item
in
}
$HTTP_SESSION_V ARS['total_price'] =
calculateprice( $HTTP_SESSION_V ARS['cart'],$dbproducts); //update total
price
$HTTP_SESSION_V ARS['items'] = calculateitems( $HTTP_SESSION_V ARS['cart']);
//update total items
}
if(isset($save) ) { //check if this is an
update
foreach ($HTTP_SESSION_ VARS['cart'] as $productid => $qty) {
//go through each item in the cart (session)
if($HTTP_POST_V ARS[$productid]=='0') { //check if
quantity set to 0
unset($HTTP_SES SION_VARS['cart'][$productid]); //if item
is removed then remove from cart array
}
else {
$HTTP_SESSION_V ARS['cart'][$productid] = $HTTP_POST_VARS[$productid];
//change quantity
}
$HTTP_SESSION_V ARS['total_price'] =
calculateprice( $HTTP_SESSION_V ARS['cart'],$dbproducts); //re claculate
price and no. items
$HTTP_SESSION_V ARS['items'] = calculateitems( $HTTP_SESSION_V ARS['cart']);
}
}
Thanks for any help
Alex
I'm trying to modify a shopping cart script from Mastering PHP/MySQL and am
having trouble setting up some arrays for it. The original code, below,
stores the cart items in a session variable array
($HTTP_SESSION_ VARS['cart'] = array();) basically this stores an associative
array with the id number of the product and the qty (ie cart[$productid] =
qty of that product).
This works fine, but I need to be able to have sub options AND colours for
some (but not all) products. I don't know exactly what to do here, I know I
need some kind of array using a combination of the productid,colou r and
option as the key for it. then storing the quantity of the item in that (and
if possible price).
Here is the code that sets up the session variables and updates the quantity
if an item is already in there (the array I'm trying to modify is the 'cart'
one, $new is the productid of the new item to add (and
$newcolour,$new options will be the colour and option for the new item)).
if($new) { //see if a product has been specified
if(!isset($HTTP _SESSION_VARS['cart'])) { //see if session variables
have been set
$HTTP_SESSION_V ARS['cart'] = array(); //if not create them
$HTTP_SESSION_V ARS['items'] = 0;
$HTTP_SESSION_V ARS['total_price'] ='0.00';
}
if(isset($HTTP_ SESSION_VARS['cart'][$new]{ //check if there is already
a value in cart array for that product
$HTTP_SESSION_V ARS['cart'][$new]++; //if so increase quantity
}
else {
$HTTP_SESSION_V ARS['cart'][$new] = 1; //otherwise just put one item
in
}
$HTTP_SESSION_V ARS['total_price'] =
calculateprice( $HTTP_SESSION_V ARS['cart'],$dbproducts); //update total
price
$HTTP_SESSION_V ARS['items'] = calculateitems( $HTTP_SESSION_V ARS['cart']);
//update total items
}
if(isset($save) ) { //check if this is an
update
foreach ($HTTP_SESSION_ VARS['cart'] as $productid => $qty) {
//go through each item in the cart (session)
if($HTTP_POST_V ARS[$productid]=='0') { //check if
quantity set to 0
unset($HTTP_SES SION_VARS['cart'][$productid]); //if item
is removed then remove from cart array
}
else {
$HTTP_SESSION_V ARS['cart'][$productid] = $HTTP_POST_VARS[$productid];
//change quantity
}
$HTTP_SESSION_V ARS['total_price'] =
calculateprice( $HTTP_SESSION_V ARS['cart'],$dbproducts); //re claculate
price and no. items
$HTTP_SESSION_V ARS['items'] = calculateitems( $HTTP_SESSION_V ARS['cart']);
}
}
Thanks for any help
Alex
Comment