Link to home
Start Free TrialLog in
Avatar of tingleweb
tinglewebFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Display total number of items from viewcart.cgi

We have a Perl, CGI website with a basket page - viewcart.cgi

What would the code be if I wanted to, display the total number of items in the basket, on the homepage please?
Avatar of Adam314
Adam314

How is data stored in the cart/basket?  Is it stored in a DB?  In an array?
If you aren't sure, post the portion of viewcart.cgi that displays all the items (the entire page isn't needed).
Avatar of tingleweb

ASKER

if (($user_id eq "") || (!(-e $local_file)) || (!(-s $local_file)))
      {
      #print "<body bgcolor=\"ffffff\">\n";
      #print "<p><p><h4 Align=Center>Your shopping cart is empty</h4><p><p>";
      open (FILE, "$root/empty_cart.htm");
      undef ($/);
      $menu=<FILE>;
      $menu=~s/\?user_id=id/?user_id=$user_id/g;
      print $menu;
      exit;
      }

unless (open (TEMPL, "$root/viewcart.tmp"))
      {
      print "<body bgcolor=\"ffffff\">\n";
      print "Cannot open template $root/viewcart.tmp";
      exit;
      }
undef ($/);
$template=<TEMPL>;
close TEMPL;
($top, $record, $bottom)=split(/<!--record-->/, $template);

$top=~s/\?user_id=id/?user_id=$user_id/g;
print $top;


unless (open (FILE, "$local_file"))
      {
      print "Cannot open $local_file";
      exit;
      }
$/="\n";
@data=<FILE>;
close FILE;
foreach $line (@data)
      {
      $line=~s/\n$//;
      foreach (split(/::/, $line))
            {
            ($name, $value)=split(/=/, $_, 2);
            $local{$name}=$value;
            if ($name ne "Quantity")
                  {
                  $hidden.="<input type=hidden name=\"$name\" value=\"$value\">\n";
                  }
            if ($name ne "ID" && $name ne "Name" && $name ne "Price" && $name ne "Quantity" && $name ne "Weight" && $name ne "location")
                  {
                  $local{'Options'}.="$name: $value<br>\n";
                  }
            }
      $local{'total_price'}=$local{'Price'}*$local{'Quantity'};
      $subtotal+=$local{'total_price'};
      $quantity+=$local{'Quantity'};
      $local{'total_price'}=&display_price($local{'total_price'});
      $local{'Price'}=&display_price($local{'Price'});
      print $hidden;
      $line1=$record;
      $line1=~s/\{\{([^\}]+)\}\}/$local{$1}/g;;
      print $line1;
      undef (%local);
      $hidden="";
      }
$data{'subtotal'}=&display_price($subtotal);
if (defined(%discount_perc))
      {
      foreach $key (keys(%discount_perc))
            {
            ($min, $max)=split(/-/, $key);
            if ((${$discount_unit}>=$min && ${$discount_unit}<=$max) || (${$discount_unit}>=$min && $max eq ""))
                  {
                  $discount=$subtotal*$discount_perc{$key}/100;
                  last;
                  }
            }
      if ($discount eq "")
            {
            $discount=$data{'discount'}="0.00";
            }
      else
            {
            $data{'discount'}=&display_price($discount);
            
            }
      }
###### members discount #################

if ($user_id=~/^m/ && defined(%m_discount_perc) && $reg_fields=~/\btotal\b/ && $show_member_discount==1)
      {
      $real_id=$user_id;
      $real_id=~s/^m//;
      unless (open (FILE, "$reg_file"))
            {
            print "Cannot open user database $reg_file\n";
            exit;
            }
      $/="\n";
      @data=<FILE>;
      close FILE;
      ($mrecord)=grep(/^$real_id\t/, @data);
      if ($mrecord eq "")
            {
            print "Your record was not found in our users database.";
            exit;
            }
      $mrecord=~s/\n$//;
      @fields=split(/\t/, $mrecord);
      @reg_fields=split(/\s*,\s*/, $reg_fields);
      for ($i=0; $i<@reg_fields; $i++)
            {
            $local{$reg_fields[$i]}=$fields[$i];
            }
      
      foreach $key (keys(%m_discount_perc))
            {
            ($min, $max)=split(/-/, $key);
            if (($local{'total'}>=$min && $local{'total'}<=$max) || ($local{'total'}>=$min && $max eq ""))
                  {
                  $mdiscount=$subtotal*$m_discount_perc{$key}/100;
                  last;
                  }
            }
      }
if ($mdiscount eq "")
      {
      $mdiscount=$data{'mdiscount'}="0.00";
      }
else
      {
      $data{'mdiscount'}=&display_price($mdiscount);
            
      }
      
##########################
$total=$subtotal-$discount-$mdiscount;
$data{'total'}=&display_price($total);
$bottom=~s/\{\{([^\}]+)\}\}/$data{$1}/g;
$bottom=~s/\?user_id=id/?user_id=$user_id/g;
print $bottom;
It looks like $quantity is already calculated.  Just display this in your HTML wherever you want it to show up.  To test:
print "Quantity=$quantity";
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am a complete novice to PERL/CGI - I didnt code this website.

How do I insert your code in amongst HTML code?  If I insert your code as it is - the code is output to the webpage.
I have added your code to viewcart.cgi and viewcart.cgi outputs the total number of items in the basket correctly but how do I insert this onto the HTML homepage?