elsif ($choice eq "add") # add item to shopping cart
{
add_item ($dbh, $cart_ref, param ("product_id"), param("quantity"), param("color"));
$page .= format_cart_html ($cart_ref, 1);
}
####
sub get_product_table
{
my $sth = shift;
my $dbh = WebDB::connect ();
my ($clref, $ref);
my @row;
my @nav_link;
my ($color, @color);
while (my $ref = $sth->fetchrow_hashref ())
{
### courtesy code written by Perl Monk LAI ####
$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};
}
my $serve_url = sprintf ("serve_image.cgi?product_id=%s;picture", escape ($ref->{product_id}));
# generate a form allowing a quantity of the item to be added
# to the cart
push (@row, start_form(-method=>'POST', -action=>url()),
hidden( -name => "choice", -override => 1,
-default => "add" ),
hidden( -name => "product_id", -override => 1,
-default => escapeHTML($ref->{product_id})),
Tr ({-style=>"font-family: verdana; font-size: 10pt;"},
td (img ({-src => $serve_url, -alt => escapeHTML($ref->{product_id})})),
td ({-width=>"80", -valign=>"top", -align => "center"}, escapeHTML( $ref->{product_id} )),
td ({-width=>"190", -valign=>"top"}, escapeHTML( $ref->{description} ))),
td (" "),
Tr ({-style=>"font-family: verdana; font-size: 10pt;"},
td ({-colspan => "3", -align => "right", -style=>"font-weight: 700; color:blue;"}, "Price: \$", escapeHTML( sprintf( "%.2f", $ref->{price}))),
Tr ({-style=>"font-family: verdana; font-size: 10pt;"},
td ({-colspan => "3", -align => "right"}, "Colors:", popup_menu( -name => "color", -values => $ref->{color}))),
Tr ({-style=>"font-family: verdana; font-size: 10pt;"},
td ({-colspan => "3", -align => "right"}, "Quantity:", popup_menu( -name => "quantity", -values => ["1", "2", "3", "4", "5", "6", "7", "8", "9"])),
td (image_button( -name => "Add Item", -src=>"../images/add.jpg", -border=>"0" )))),
Tr ({-valign=>"top"},
td ({-colspan=>"7", -width=>"100%"}, hr)),
end_form()
);
}
$sth->finish ();
return undef unless @row; # no items?
return (table ({-border => 0, -align => "center", -style=>"font-family: verdana; font-size: 10pt;"}, @row));
}
####
sub add_item
{
my ($dbh, $cart_ref, $product_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.
if (!exists ($cart_ref->{$product_id}))
{
my $sth = $dbh->prepare ("SELECT * FROM catalog_pet WHERE product_id = ?");
$sth->execute ($product_id);
my $item_ref = $sth->fetchrow_hashref ();
$sth->finish ();
return if !defined ($item_ref); # this shouldn't happen...
$cart_ref->{$product_id} = {}; # create new entry, indexed by item ID
$cart_ref->{$product_id}->{description} = $item_ref->{description};
$cart_ref->{$product_id}->{price} = $item_ref->{price};
$cart_ref->{$product_id}->{qty} = 1;
}
$cart_ref->{$product_id}->{qty} = $qty;
$cart_ref->{$product_id}->{color} = $color;
}
####
sub format_cart_html
{
my ($cart_ref, $show_links) = @_;
my $total_price = 0;
my @row;
if (!keys (%{$cart_ref}))
{
return (p ("Shopping cart is empty."));
}
$page .= h3 ({-align=>"center", -style=>"font-family: verdana; font-size: 16pt; color: blue;"}, ("Shopping Cart"));
push (@row, Tr ({-align => "CENTER", -valign=>"TOP", -BGCOLOR=>"silver", -style=>"font-family: verdana; font-size: 10pt;"},
th ({-width=>"50"},("Item")),
th ({-width=>"50"},("Qty")),
th ({-width=>"90"},("Color")),
th ({-width=>"190"},("Description")),
th ({-width=>"94"},("Unit Price")),
th ({-width=>"94"},("Price"))
));
foreach my $product_id (sort (keys (%{$cart_ref})))
{
my $item_ref = $cart_ref->{$product_id};
my $total_item_price = $item_ref->{qty} * $item_ref->{price};
$total_price += $total_item_price;
# generate a link allowing the item to be deleted from the cart
my $url = sprintf ("%s?choice=delete;product_id=%s",
url (), escape ($product_id));
push (@row, start_form(-method=>'GET', -action=>url()),
hidden( -name => "choice", -override => 1, -default => "update" ),
hidden( -name => "product_id", -override => 1, -default => escapeHTML( $product_id )),
Tr ({-valign=>"center", -style=>"font-family: verdana; font-size: 10pt;"},
td ({-align => "center"},(escapeHTML ($product_id))),
td ({-align => "center"},( textfield( -name => "quantity", -size => "1", -override => 1, -value => $item_ref->{qty}))),
td (escapeHTML ($item_ref->{color})),
td (escapeHTML ($item_ref->{description})),
td ({-align => "right"},
escapeHTML (sprintf ("%.2f", $item_ref->{price}))),
td ({-align => "right"},
escapeHTML (sprintf ("%.2f", $total_item_price))),
td (a ({-href => $url}, img ({-src => "../images/delete1.jpg", -border => "0"}))),
td (image_button( -name => "update", -src=>"../images/update1.jpg", -border=>"0" )),
end_form()
));
}
push (@row, Tr ({-align => "CENTER", -valign=>"center", -style=>"font-family: verdana; font-size: 10pt;"},
td ({-colspan => "2"}, ""),
td ({-colspan => "3", -align=>"right", -style=>"font-weight: 700;"}, "Total"),
td ({-align => "right", -style=>"font-weight: 700;"},
escapeHTML (sprintf ("%.2f", $total_price)))
));
return (table ({-align => "CENTER", -border => 0}, @row));
}