updating values in a $_SESSION array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Håvard Olerud Eriksen

    updating values in a $_SESSION array

    I've been working on a webshop, and I've got most of the functionality up
    and running. One problem, however, that I don't seem to be able to solve is
    as follows. My shopping cart is stored in $_SESSION['cart'] array. As I need
    to keep tabs on what items are ordered and their quantity I add elements
    like this:
    array_push($_SE SSION['cart'],array($item => $quantity));
    If I add three elements and then var_dump the global variable it spits out:
    array(3) { [0]=> array(1) { ["Boxers"]=> int(4) } [1]=> array(1) {
    ["T-shirt"]=> int(1) } [2]=> array(1) { ["Socks"]=> int(45) } }

    These value pairs are taken out like this:

    foreach($_SESSI ON[cart] as $item => $quantity)
    {
    for($i = 0; $i < 1; $i++) // yes I know about this lame loop, but it
    doesn't work without it
    {
    while (list( $product , $currQuantity) = each($quantity) )
    {
    showCartItems($ product, $currQuantity); // Iterere gjennom alle varene
    i kurven
    }
    }
    }
    Sorry about the long code, but I just wanted to explain it properly.
    My problem, however, is to update the elements in the array like eg. change
    the quantity of socks in the above array from 45 to 120. This has left me
    completely dumbfounded. The convoluted (imho) $_SESSION array makes
    manipulations like this hard for a sort of newbie like me.
    Does anyone know how to do this, or alternatively an easier way of storing
    the array elements?
    They basically need to be stored like a std::C++ pair, array[0] item =>
    quantity, array[1] item => quantity.

    I've been stuck on this for ages now so any helg is much appreciated.

    Cheers,
    Håvard


  • Jan Pieter Kunst

    #2
    Re: updating values in a $_SESSION array

    In article <3f676274$1@new s.broadpark.no> ,
    "Håvard Olerud Eriksen" <hereriks@PANTS yahoo.com> wrote:
    [color=blue]
    > Does anyone know how to do this, or alternatively an easier way of storing
    > the array elements?[/color]

    initialize the array with:

    $_SESSION['cart'] = array();

    then add items like this:

    $_SESSION['cart']['Boxers'] = 4;
    $_SESSION['cart']['T-shirts'] = 2;
    $_SESSION['cart']['Socks'] = 45;

    It gives you a much simpler array structure:

    print_r($_SESSI ON['cart']);

    Array
    (
    [Boxers] => 4
    [T-shirts] => 2
    [Socks] => 45
    )

    Simply update the quantities with:

    $_SESSION['cart']['Socks'] = $new_quantity;
    $_SESSION['cart']['Socks'] += $quantity;

    or similar.

    HTH,
    JP

    --
    Sorry, <devnull@cauce. org> is een "spam trap".
    E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

    Comment

    • Håvard Olerud Eriksen

      #3
      Re: updating values in a $_SESSION array

      > Array[color=blue]
      > (
      > [Boxers] => 4
      > [T-shirts] => 2
      > [Socks] => 45
      > )
      >
      > Simply update the quantities with:
      >
      > $_SESSION['cart']['Socks'] = $new_quantity;
      > $_SESSION['cart']['Socks'] += $quantity;[/color]

      Thanks a lot for the quick answer. Well, this certainly make things a lot
      easier. Also stuff like checking for an existing key, then updating the
      quantity of that instead of making a separate array element etc. will be a
      breeze.

      Cheers,
      Håvard


      Comment

      Working...