Ask a Question related to PHP Development, Design and Development.
-
Håvard Olerud Eriksen #1
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
-
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... -
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... -
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... -
[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... -
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... -
Håvard Olerud Eriksen #2
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
-
Jan Pieter Kunst #3
Re: updating values in a $_SESSION array
In article <3f676274$1@news.broadpark.no>,
"Håvard Olerud Eriksen" <hereriks@PANTSyahoo.com> wrote:
initialize the array with:> Does anyone know how to do this, or alternatively an easier way of storing
> the array elements?
$_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
-
Jan Pieter Kunst #4
Re: updating values in a $_SESSION array
In article <3f676274$1@news.broadpark.no>,
"Håvard Olerud Eriksen" <hereriks@PANTSyahoo.com> wrote:
initialize the array with:> Does anyone know how to do this, or alternatively an easier way of storing
> the array elements?
$_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
-
Håvard Olerud Eriksen #5
Re: updating values in a $_SESSION array
> Array
Thanks a lot for the quick answer. Well, this certainly make things a lot> (
> [Boxers] => 4
> [T-shirts] => 2
> [Socks] => 45
> )
>
> Simply update the quantities with:
>
> $_SESSION['cart']['Socks'] = $new_quantity;
> $_SESSION['cart']['Socks'] += $quantity;
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
-
Håvard Olerud Eriksen #6
Re: updating values in a $_SESSION array
> Array
Thanks a lot for the quick answer. Well, this certainly make things a lot> (
> [Boxers] => 4
> [T-shirts] => 2
> [Socks] => 45
> )
>
> Simply update the quantities with:
>
> $_SESSION['cart']['Socks'] = $new_quantity;
> $_SESSION['cart']['Socks'] += $quantity;
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



Reply With Quote

