updating values in a $_SESSION array

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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($_SESSION['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($_SESSION[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


    Håvard Olerud Eriksen Guest

  2. Similar Questions and Discussions

    1. Updating multiple values from a query
      Hi all, I'm looking for a way to update multiple lines in a database using a form. While I've had no trouble updating single records, this whole...
    2. copying a multidimensional array to $_SESSION
      Probably a simple question but I can't find the answer anyway. Specifically, is it possible to copy a multidimensional array into the $_SESSION...
    3. Updating dataset with values from TextBox control
      Hi I have built a very simple page, which takes data via TextBox controls, and updates to a Dataset. I've used the Data Access Blocks (v2), to...
    4. [PHP] Passing Values between Pages : $_SESSION / $_GET
      My question here is " How do I get the name of the company that was passed to this script as a $_GET Parameter. I cannot use the $company because...
    5. Passing Values between Pages : $_SESSION / $_GET
      Hello All, I am using php and mysql in my application which allows users to search/query a database. The database is cast and has about 32 rows...
  3. #2

    Default 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($_SESSION['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($_SESSION[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


    Håvard Olerud Eriksen Guest

  4. #3

    Default Re: updating values in a $_SESSION array

    In article <3f676274$1@news.broadpark.no>,
    "Håvard Olerud Eriksen" <hereriks@PANTSyahoo.com> wrote:
    > Does anyone know how to do this, or alternatively an easier way of storing
    > the array elements?
    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($_SESSION['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" = @.
    Jan Pieter Kunst Guest

  5. #4

    Default Re: updating values in a $_SESSION array

    In article <3f676274$1@news.broadpark.no>,
    "Håvard Olerud Eriksen" <hereriks@PANTSyahoo.com> wrote:
    > Does anyone know how to do this, or alternatively an easier way of storing
    > the array elements?
    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($_SESSION['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" = @.
    Jan Pieter Kunst Guest

  6. #5

    Default Re: updating values in a $_SESSION array

    > Array
    > (
    > [Boxers] => 4
    > [T-shirts] => 2
    > [Socks] => 45
    > )
    >
    > Simply update the quantities with:
    >
    > $_SESSION['cart']['Socks'] = $new_quantity;
    > $_SESSION['cart']['Socks'] += $quantity;
    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


    Håvard Olerud Eriksen Guest

  7. #6

    Default Re: updating values in a $_SESSION array

    > Array
    > (
    > [Boxers] => 4
    > [T-shirts] => 2
    > [Socks] => 45
    > )
    >
    > Simply update the quantities with:
    >
    > $_SESSION['cart']['Socks'] = $new_quantity;
    > $_SESSION['cart']['Socks'] += $quantity;
    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


    Håvard Olerud Eriksen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139