trouble with arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Hopson

    trouble with arrays

    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



  • Jeffrey Silverman

    #2
    Re: trouble with arrays

    On Mon, 21 Jun 2004 14:12:53 +0100, Alex Hopson wrote:
    [color=blue]
    > Thanks for any help[/color]

    I'm not sure what you are doing, or specifically what you are asking. I
    do, however, have a suggestion that will greatly simplify your work with
    PHP session variables.

    Use the $_SESSION array instead of the $HTTP_SESSION_V ARS array!

    See the following:





    I mention this because a lot of tutorials, books, and articles about PHP 4
    fail to mention the existence of the $_SESSION array because they are
    outdated. However, this array has been available since PHP 4.1.x which is
    over two years old!

    allright, later...

    (Clarify your original question if you want a more specific answer.)

    --
    Jeffrey D. Silverman | jeffrey AT jhu DOT edu
    Website | http://www.wse.jhu.edu/newtnotes/

    Comment

    • Jay Donnell

      #3
      Re: trouble with arrays

      Why don't you create an object/class with all the data you need and
      store that in $_SESSION['cart'] instead of a multidimensiona l array.
      It will be a little slower, but a lot easier to deal with imho.

      Jay

      "Alex Hopson" <alex*under_sco re*hopson@hotma il.com> wrote in message news:<j3BBc.375 93$JT2.17055@fe 26.usenetserver .com>...[color=blue]
      > 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[/color]

      Comment

      Working...