Array. setting item id for session based cart?

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

    Array. setting item id for session based cart?

    Hello,

    I'm hoping someone can help me out here.
    I'm building a shopping cart using sessions, populating with $_POST
    values from a form. Code abbreviated below:

    check if form is submitted. if so, set up variables:

    $name = $_POST['name'];
    $ribbon = $_POST['ribbon'];
    $quantity = 1;
    if (isset($_SESSIO N['cart'][$item_id])) $item_id = ($item_id+1);
    else $item_id = 1;

    now, add those values to cart array:

    $_SESSION['cart'][]= array( 'name' =$name, 'ribbon' =$ribbon,
    'quantity' =$quantity, 'item_id' =$item_id);
    $cart = $_SESSION['cart'];

    This is working mostly so far. Problem is, assigning an 'item id' to
    each item in the array is failing. I'll need this later to allow
    users to update quantities,dele te, etc. from the cart. I'm thinking
    that I'd do well to assign the array numbers (0 and 1 as shown in cart
    array structure below) as an 'item id' in each item's array.
    Unfortunately, I can't figure out how to do this. You'll see below
    that each array's 'item_id" is getting stuck at 1. Anyone out there
    have any suggestions? Thanks so much in advance, Chris.

    Array
    (
    [0] =Array
    (
    [name] =Bob
    [ribbon] =green multi
    [quantity] =1
    [item_id] =1
    )

    [1] =Array
    (
    [name] =Sally
    [ribbon] =blue multi
    [quantity] =1
    [item_id] =1
    )

    )
  • Curtis

    #2
    Re: Array. setting item id for session based cart?

    chrism wrote:
    Hello,
    >
    I'm hoping someone can help me out here.
    I'm building a shopping cart using sessions, populating with $_POST
    values from a form. Code abbreviated below:
    >
    check if form is submitted. if so, set up variables:
    >
    $name = $_POST['name'];
    $ribbon = $_POST['ribbon'];
    $quantity = 1;
    if (isset($_SESSIO N['cart'][$item_id])) $item_id = ($item_id+1);
    else $item_id = 1;
    >
    [snip]
    >
    This is working mostly so far. Problem is, assigning an 'item id' to
    each item in the array is failing. I'll need this later to allow
    users to update quantities,dele te, etc. from the cart. I'm thinking
    that I'd do well to assign the array numbers (0 and 1 as shown in cart
    array structure below) as an 'item id' in each item's array.
    Unfortunately, I can't figure out how to do this. You'll see below
    that each array's 'item_id" is getting stuck at 1. Anyone out there
    have any suggestions? Thanks so much in advance, Chris.
    Is $item_id defined elsewhere in your code? From the snippet above, it
    looks as if you're using an undefined variable (try testing with
    error_level = E_ALL in your development environment), which would
    result in your if-statement always failing.

    --
    Curtis

    Comment

    • chrism

      #3
      Re: Array. setting item id for session based cart?

      Thanks Curtis. Yes, I defined $item_id earlier on. What I'm really
      trying to figure out is how to set the array value $item_id equal to
      the array identifier (sorry for the lack of proper terminology) as
      show below. (I'd like the first array's item_id to be set to 0, and
      the second array's item_id to be set to 1. this way I'll have
      something to refer to the arrays by later when i need to update the
      cart.)

      Array
      (
      [0] =Array
      (
      [name] =Bob
      [ribbon] =green multi
      [quantity] =1
      [item_id] =1
      )

      [1] =Array
      (
      [name] =Sally
      [ribbon] =blue multi
      [quantity] =1
      [item_id] =1
      )

      )

      Comment

      • Geoff Berrow

        #4
        Re: Array. setting item id for session based cart?

        Message-ID:
        <0e405060-4d9f-4f1f-b26a-634f550254fd@u2 8g2000hsc.googl egroups.comfrom
        chrism contained the following:
        >Thanks Curtis. Yes, I defined $item_id earlier on. What I'm really
        >trying to figure out is how to set the array value $item_id equal to
        >the array identifier (sorry for the lack of proper terminology) as
        >show below. (I'd like the first array's item_id to be set to 0, and
        >the second array's item_id to be set to 1. this way I'll have
        >something to refer to the arrays by later when i need to update the
        >cart.)

        You don't need to. The session array key (that's the word you are
        searching for) will increment automatically. You don't need an item id
        at all. In fact, a lot of your code is redundant.

        All you need is.

        $_SESSION['cart'][]= array( 'name' =$_POST['name'], 'ribbon' =>
        $_POST['ribbon'],'quantity' =1);

        Although you could have 'quantity' =$_POST['quantity'] if you have
        the appropriate form element.

        When you loop through the array of cart items to display the contents:

        foreach($_SESSI ON['cart'] as $key=>$value){
        //Loop the array, $value, to show the item details. $key will contain
        //the key identifying the item. Pass it back by adding it as a query
        //string to a url e.g.
        //<a href='edit cart_items.php? item=$key'>edit cart item</a>
        }

        --
        Geoff Berrow 011000100110110 0010000000110
        001101101011011 001000110111101 100111001011
        100110001101101 111001011100111 010101101011
        http://slipperyhill.co.uk - http://4theweb.co.uk

        Comment

        • chrism

          #5
          Re: Array. setting item id for session based cart?

          This is great. Thanks a lot, Geoff.

          Comment

          Working...