in reply to Re: Re: Need duplicate item entries in shopping cart
in thread Need duplicate item entries in shopping cart
Just to give you an idea of what you need to do to that code to change it to arrays, here is another take on your add_item sub:
Note that it is no longer possible to look up a single item in the cart by ID with this structure: you have to loop through all the items.sub add_item { my ($dbh, $cart_ref, $item_id, $qty, $color) = @_; # If the item isn't already in the cart, look it up from the datab +ase # and store it in the cart as a new entry with a quantity of zero. my $saw_item = 0; foreach my $item (@{$cart_ref}) { if (($item->{'id'} == $item_id) && $item->{'color'} eq $color) +) { $saw_item = 1; break; } } if (!$saw_item) { my $sth = $dbh->prepare ("SELECT item_id, description, categor +y, price, picture, thumbnail, mime_type, color FROM catalog_pet WHERE + item_id = ?"); $sth->execute ($item_id); my $row = $sth->fetchrow_hashref (); return if !defined ($row); # this shouldn't happen... my $item_ref = {}; $item_ref->{description} = $row->{description}; $item_ref->{price} = $row->{price}; $item_ref->{qty} = 1; $item_ref->{color} = $color; } $item_ref->{qty} = $qty; push (@{$cart_ref}, @item_ref); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Need duplicate item entries in shopping cart
by b310 (Scribe) on Feb 20, 2003 at 14:32 UTC | |
by perrin (Chancellor) on Feb 20, 2003 at 18:09 UTC |