trouble with arrays

From: Alex Hopson (alex*under_score*hopson_at_hotmail.com)
Date: 06/21/04

  • Next message: Andrew: "Serialize / Unserialize"
    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


  • Next message: Andrew: "Serialize / Unserialize"

    Relevant Pages

    • Re: "PHP and MySQL Web Development" by Luke Welling and Laura Thomson
      ... > adding something to the cart, and seeing the item in the cart, when then ... been supplanted, more recently, by the $_SESSION array. ... Look for "errata" for the book on the book publisher's website. ... Jeffrey D. Silverman | jeffrey AT jhu DOT edu ...
      (alt.php)
    • Re: array type problem
      ... >> array and prints nothing. ... This works in as much as it displays the cart. ... when you run it the second time, even though there is only one item in the ... Andy ...
      (comp.lang.php)
    • Re: [PHP] Help with a foreach statement
      ... dual-demesnional array in $cart. ... My ECHO statement should look like this: ... PHP General Mailing List ...
      (php.general)
    • Problem appending arrays in shopping cart
      ... I am creating a shopping cart using PHP Version 4.1.2. ... registering a cart object in a session. ... the $order array is always overwritten with the ... the session and calls the add_itemfunction. ...
      (alt.php)
    • Problem appending arrays in shopping cart
      ... I am creating a shopping cart using PHP Version 4.1.2. ... registering a cart object in a session. ... the $order array is always overwritten with the ... the session and calls the add_itemfunction. ...
      (comp.lang.php)