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 database # 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, category, 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); }