By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,968 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,968 IT Pros & Developers. It's quick & easy.

Array. setting item id for session based cart?

P: n/a
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
)

)
Oct 16 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Oct 16 '08 #2

P: n/a
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
)

)
Oct 16 '08 #3

P: n/a
Message-ID:
<0e**********************************@u28g2000hsc. googlegroups.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($_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
Oct 16 '08 #4

P: n/a
This is great. Thanks a lot, Geoff.

Oct 16 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.