trouble with arrays
From: Alex Hopson (alex*under_score*hopson_at_hotmail.com)
Date: 06/21/04
- Previous message: JB: "Enable Mail function for PHP under Windows XP"
- Next in thread: Jeffrey Silverman: "Re: trouble with arrays"
- Reply: Jeffrey Silverman: "Re: trouble with arrays"
- Reply: Jay Donnell: "Re: trouble with arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 21 Jun 2004 14:12:53 +0100
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,colour 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,$newoptions 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_VARS['cart'] = array(); //if not create them
$HTTP_SESSION_VARS['items'] = 0;
$HTTP_SESSION_VARS['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_VARS['cart'][$new]++; //if so increase quantity
}
else {
$HTTP_SESSION_VARS['cart'][$new] = 1; //otherwise just put one item
in
}
$HTTP_SESSION_VARS['total_price'] =
calculateprice($HTTP_SESSION_VARS['cart'],$dbproducts); //update total
price
$HTTP_SESSION_VARS['items'] = calculateitems($HTTP_SESSION_VARS['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_VARS[$productid]=='0') { //check if
quantity set to 0
unset($HTTP_SESSION_VARS['cart'][$productid]); //if item
is removed then remove from cart array
}
else {
$HTTP_SESSION_VARS['cart'][$productid] = $HTTP_POST_VARS[$productid];
//change quantity
}
$HTTP_SESSION_VARS['total_price'] =
calculateprice($HTTP_SESSION_VARS['cart'],$dbproducts); //re claculate
price and no. items
$HTTP_SESSION_VARS['items'] = calculateitems($HTTP_SESSION_VARS['cart']);
}
}
Thanks for any help
Alex
- Previous message: JB: "Enable Mail function for PHP under Windows XP"
- Next in thread: Jeffrey Silverman: "Re: trouble with arrays"
- Reply: Jeffrey Silverman: "Re: trouble with arrays"
- Reply: Jay Donnell: "Re: trouble with arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|