Yes, I'm aware of that. What I need to do is store the entries into the cart indexed by item_id, which distinguishes a product from one color to another.
When I try to bring in item_id with this statement of code:
$clref = $dbh->prepare ("SELECT color FROM item WHERE prod_id = ".$ref
+->{product_id});
$clref->execute();
while (my $color = $clref->fetchrow_hashref()) {
push @{$ref->{color}}, $color->{color};
}
And, then carry everything over to my Add Item subroutine, I receive Shopping Cart is Empty message.
Now, here is what I did to the code to try to bring in the item_id and which later produces an empty shopping cart message.
$clref = $dbh->prepare ("SELECT item_id, color FROM item WHERE prod_id
+ = ".$ref->{product_id});
$clref->execute();
while (my $color, $item_id = $clref->fetchrow_hashref()
+) {
push @{$ref->{color}, {$ref->{item_id}}, $color->{color
+}, $item_id->{item_id};
}
I'm trying to create a $ref->{item_id} so it is the same as all the other variables. Then, I'm trying to pass the $ref->{item_id} to the add_item subroutine.
It is quite apparent that I'm goofing up somewhere. I just don't know where.
Here's also what I've done to the add_item subroutine...
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.
if (!exists ($cart_ref->{$item_id}))
{
my $sth = $dbh->prepare ("SELECT * FROM item, catalog_pet WHER
+E catalog_pet.product_id = item.prod_id AND catalog_pet.product_id =
+?");
$sth->execute ($item_id);
my $item_ref = $sth->fetchrow_hashref ();
$sth->finish ();
return if !defined ($item_ref); # this shouldn't happen...
$cart_ref->{$item_id} = {}; # create new entry, indexed by
+item ID
$cart_ref->{$item_id}->{description} = $item_ref->{description
+};
$cart_ref->{$item_id}->{price} = $item_ref->{price};
$cart_ref->{$item_id}->{qty} = 1;
}
$cart_ref->{$item_id}->{qty} = $qty;
$cart_ref->{$item_id}->{color} = $color;
}
You can see that I'm trying to store the cart entries by item_id. This attempt of coding produces an empty shopping cart message.
|