Tom , thanks for you analysis for me.

>I hope you're keeping your product data and prices in a database, with
>this merely being example code.

Dear Tom , yes you are correct.  i am playing it for the test. the
final one will keep in mysql db.
the whole working code is picup from
http://search.cpan.org/~sherzodr/CGI-Session-3.95/Session/CookBook.pm
( shopping cart section)
which is a simple sample code for using cgi::session.


the sample code is a workable one. however when adding a item to the
cart. the script will merely push the new data to the array which will
causes mutiple
same items been listed.

much like this

item1  priice1  1
item1  priice1  1
item1  priice1  1
item2  priice2  quantity


actually i wish the script to push the value only if the itemID is not
exist in cart. or just using splice to update the quantity of the
item. i just can not figure out how the code is .  below is what i
expected result.
item1  priice1  3
item2  priice2  quantity


>I suspect that you want to handle the lack of itemID in some special
>way. How you handle it depends upon what it means, though; what does
>it mean to your application that the itemID is missing? Does it mean
>that the user has mis-filled the form, or does it mean that your
>program is being invoked before the user can fill the form?
Normally itemID should be presented. just in case. i have changed a
little bit to see what error it will returned. and also remove the
path infos come along with the error returned.



here is the code i have


use CGI::Session;
use Data::Dumper;
use CGI;
use lib 'D:\webtest\merchant\linkcart\admin';
use Links qw/:objects/; # or whatever $IN, $DB, $CFG, $USER etc...
use Links qw/$USER/;
Links::init('D:\webtest\merchant\linkcart\admin'); ########
Links::init_user(); # gets the user information



my $cgi = new CGI;
my $session = new CGI::Session(undef, $cgi, {Directory=>'F:/tmp'});
print $session->header();

 # Imaginary database in the form of a hash
    %products = (
        1001 =>    [ "1_usr/bin/perl t-shirt",    14.99],
        1002 =>    [ "2_just perl t-shirt",       21],
        1003 =>    [ "3_shebang hat",             31],
        1004 =>    [ "4_linux mug",               41],
          1005 =>    [ "5_itme5",               51],
        # on and on it goes....
    );


 # getting the run mode for the state. If doesn't exist,
    # defaults to "display", which shows the cart's content
    $cmd = $cgi->param("cmd") || "display";

    if ( $cmd eq "display" ) {
        print display_cart($cgi, $session);

    } elsif ( $cmd eq "add" ) {
        print add_item($cgi, $session, \%products,);

    } elsif ( $cmd eq "remove") {
        print remove_item($cgi, $session);

    } elsif ( $cmd eq "clear" ) {
        print clear_cart($cgi, $session);

    } else {
        print display_cart($cgi, $session);

    }



sub display_cart {
       print "  $itemID $idx $itemIDnum  <br><br><br><br>\n";
my ($cgi, $session) = @_;
# getting the cart's contents
my $cart = $session->param("CART") || [];
my $total_price = 0;
my $RV = qq~<table><tr><th>Title</th><th>Price</th><th>Quantity</th></
tr>~;
if ( $cart ) {
my $idx = 0;
for my $product ( @{$cart} ) { $idx++;
#$total_price += $product->{price};
$subtotal = ($product->{price})*($product->{quantity});
$total_price += $subtotal;
# my $idx = 0;
$idx1 = ($idx+1);
$RV .= qq~<tr><td>$product->{name}</td><td>$product->{price} (item\#
$idx  <a href="http://2florist.org/linkcart/admin/linktest/cart.cgi?
cmd=remove;itemID=$product->{itemID};itemIDnum=$idx">XX</a> )</td><td>
$subtotal----$product->{quantity}</td></tr>~;
}
} else {
$RV .= qq~
<tr>
<td colspan="2">There are no items in your cart
yet</td>
</tr>~;
}
$RV .= qq~
<tr>
<td><b>Total Price:::</b></td>
<td><b>$total_price></b></td>
</tr></table>~;

$RV .= qq~
<BR><BR><BR><BR>
<a href="http://2florist.org/linkcart/admin/linktest/cart.cgi?
cmd=add;itemID=1001">+1001</a> <BR><BR>
<a href="http://2florist.org/linkcart/admin/linktest/cart.cgi?
cmd=add;itemID=1002">+1002</a> <BR><BR>
<a href="http://2florist.org/linkcart/admin/linktest/cart.cgi?
cmd=add;itemID=1003">+1003</a> <BR><BR>
<a href="http://2florist.org/linkcart/admin/linktest/cart.cgi?
cmd=add;itemID=1004">+1004</a> <BR><BR>
<a href="http://2florist.org/linkcart/admin/linktest/cart.cgi?
cmd=add;itemID=1005">+1005</a> <BR><BR>
~;

return $RV;

}



sub add_item {
  my ($cgi, $session, $products) = @_;
 # getting the itemID to be put into the cart
  my $itemID = $cgi->param("itemID")  ||   &error ("$!  ( No item
specified ) ");
        # getting the current cart's contents:
         my $cart = $session->param("CART") || [];


for my $product ( @{$cart} )  {


 if ( $product->{itemID} == $itemID) {  updateitem_quantity }
else (
 push @{ $cart }, {
            itemID => $itemID,
            name   => $products->{$itemID}->[0],
            price  => $products->{$itemID}->[1],
         quantity  => $quantity


        };



}


sub updateitem_quantity {  # not yet coding now }



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to