Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array. setting item id for session based cart?

1 view
Skip to first unread message

chrism

unread,
Oct 15, 2008, 10:15:29 PM10/15/08
to
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($_SESSION['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,delete, 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

unread,
Oct 15, 2008, 10:30:16 PM10/15/08
to
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($_SESSION['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,delete, 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

chrism

unread,
Oct 16, 2008, 6:17:33 AM10/16/08
to
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.)

Geoff Berrow

unread,
Oct 16, 2008, 6:56:01 AM10/16/08
to
Message-ID:
<0e405060-4d9f-4f1f...@u28g2000hsc.googlegroups.com> from
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($_SESSION['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 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk

chrism

unread,
Oct 16, 2008, 11:46:04 AM10/16/08
to
This is great. Thanks a lot, Geoff.

0 new messages