After days of frustration, I must turn to the list for some help. Below
is my code for the start of a rewrite of a shopping cart. I am rewriting
it to CGI::App with CGI::App:Sess. I don't know what Im doing wrong and
about to give up. I cat get the hash ref that the cart is in to store in
the session table. See anything?
#!/usr/bin/perl -w
use strict;
use lib './';
use CGI::Carp qw(fatalsToBrowser);
use Store::Customer;
my $customer=Store::Customer->new();
$customer->run();
package Store;
use base 'CGI::Application';
use CGI::Application::Session;
use DBTools;
sub cgiapp_init {
my $self = shift;
# Initialize Variables
my $query = $self->query;
my ($cart_ref, $session);
# Initialize Databasew Connection and put in paraemeter space
$self->param('dbh' => DBTools->connect());
my $dbh=$self->param('dbh');
$self->session_config(
CGI_SESSION_OPTIONS => [ "driver:MySQL", $self->query,
{Handle=>$dbh} ],
COOKIE_PARAMS => {
-path => '/',
},
SEND_COOKIE => 1,
);
# Extract the shopping cart from the session, if there isn't one yet
create it
if (!defined ($cart_ref=$self->session->param("cart"))) {
# The cart is a hash reference of items indexd by item id
$cart_ref={};
}
}
sub teardown {
my $self=shift;
# If the session still exists, store the cart back into it
my $cart_count=0;
my $session=$self->session;
my $cart_ref=$session->param('cart');
if (defined($session)) {
$session->param("cart", $cart_ref);
$session->close();
}
#Disconnect because we are done
$self->param('dbh')->disconnect();
}
1; # KEEP PERL HIP-HOP-HAPPY
package Store::Customer;
#
# LOOK UP HTML FORM BUILDER
#
#
use base 'Store';
#use CGI qw{:standard};
sub setup {
my $self=shift;
$self->start_mode('add_item');
$self->mode_param('shopping_action');
$self->run_modes (
'view_cart' => 'view_cart',
'add_item' => 'add_item',
);
}
sub add_item {
my $self=shift;
my $q=$self->query();
my $dbh=$self->param('dbh');
# Retrieve Session and get the cart
my $session=$self->session;
my $cart_ref=$session->param("cart");
# Get the current Item Number from the input form
my $item_id=$q->param('item_id');
my $customer_id=$q->param('customer_id');
# Required form parameters passed into cart
my @keys_to_get=('customer_id', 'qty', 'item_name', 'price',
'return_url', 'taxable');
my $sth=$dbh->prepare("SELECT parameter FROM user_parameters WHERE
customer_id='$customer_id'");
$sth->execute();
while (my $row=$sth->fetchrow_hashref()) {
push (@keys_to_get, $row->{parameter});
}
unless ($item_id eq "" || !defined($item_id)) {
if (!exists ($cart_ref->{$item_id})) {
$cart_ref->{$item_id}={};
foreach (@keys_to_get) {
$cart_ref->{$item_id}->{$_}=$q->param("$_");
}
}
++$cart_ref->{$item_id}->{qty};
}
}
sub view_cart {
my $self=shift;
my $q=$self->query();
my $session=$self->session;
my $cart_ref=$session->param("cart");
my $total_price=0;
my @row;
my $page;
my $total_qty;
my $return_to;
return ("<h2>Shopping Cart is Empty!</h2>") if (!keys
(%{$cart_ref}));
push (@row, $q->Tr({-style=>"background-color:#CCCC66;
color:black"},
$q->td("Quantity"),
$q->td("Product Number"),
$q->td("Product Name"),
$q->td("Description"),
$q->td("Unit Price"),
$q->td("Unit Total"),
));
foreach my $item_id (sort (keys (%{$cart_ref}))) {
my $item_ref=$cart_ref->{$item_id};
my $qty_name="qty".$item_id;
my
$total_item_price=$item_ref->{qty}*$item_ref->{price};
$total_price += $total_item_price;
$total_qty += $item_ref->{qty};
$return_to=$item_ref->{returnto};
my $url_delete=sprintf("%s?choice=delete;item_id=%s",
$q->url(), $q->escape($item_id));
push (@row,
$q->Tr({-style=>"background-color:#CCCCCC;"},
$q->td
($q->textfield({-name=>"$qty_name", -size=>'3', -value=>$q->escapeHTML
($item_ref->{qty})}), "<input type='submit' name=choice value='Update'>
<a href=$url_delete>Delete</a>"),
$q->td ($q->escapeHTML
($item_ref->{ID})),
$q->td ($q->escapeHTML
($item_ref->{name})),
$q->td ($q->escapeHTML
($item_ref->{description}), "<br>", $item_ref->{color}, "<br>",
$item_ref->{size}, "<br>", $item_ref->{finish}, "<br>",
$item_ref->{style}),
$q->td ($q->escapeHTML (sprintf ("%.2f",
$item_ref->{price}))),
$q->td ($q->escapeHTML (sprintf ("%.2f",
$total_item_price)))
));
}
# $total_price += get_shipping($total_qty);
push (@row, $q->Tr({-style=>"background-color:#CCCCCC;"},
$q->td ({-colspan=>"3"}, ""),
$q->td ({-colspan=>"2"}, "Total"),
$q->td ({-align=>"right"}, $q->escapeHTML
(sprintf ("%.2f", $total_price)))
));
$page .= "<form
action='https://secure.bigdawgcommunications.com/~woodbyma/cgi-bin/bd_ca
rt.cgi' method=POST>";
$page .= $q->table({-width=>"100%", -style=>"font-size:12px"},
@row);
unless ($show_links == 2) {
$page .= "<input type='submit' name='choice'
value='Checkout'><a href=$return_to>Continue Shopping</a><br><br>";
}
$page .= "</form>";
#print header();
return $page;
}
1;
Thanks for any help.
Scott
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/suppressed/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed
Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.