Fellow monks,
I am currently writing a shopping cart for my dads floral website, but i'm having some problems with the below chunk of code, this is the entire additem subroutine, the part im having problems with i commented, what its suppose to do is check to see if the item is already in the cart, if it is, just update the quantity instead of adding a duplicate entry...but it just adds a dup entry so i guess its not recognizing that $in is equal to $item for some reason, maybe someone can see my problem, any help is greatly appreciated
sub additem{
my $item = shift;
my $quant = shift;
if(!(-e $custid)){
open INF, ">$custid" or die "Couldnt create cart file: $!\n";
print INF '';
close INF;
}
open INF, "products.db" or die "Couldnt open db:$!\n";
my @products = <INF>;
close INF;
my ($itemno,$prod,$price,$desc);
for(@products){
($itemno,$prod,$price,$desc) = split(/\|/,$_);
if($itemno == $item){
last;
}
}
open CUSTCART, "$custid" or die "Couldnt read cart file: $!\n";
my @custchoices = <CUSTCART>;
close CUSTCART;
for(@custchoices){
chomp;
my($in,$pr,$pri,$de,$qu) = split(/\|/,$_);
if($in == $item){ #this is the start of my problems ****
$qu++;
open INF, ">$custid" or die "Couldnt update quantity: $!\n";
print INF "$in|$pr|$pri|$de|$qu|\n";
foreach my $line(@custchoices){
if($item !~ /^$in/){
print INF $line;
}
}
close INF;
print "Location: $url=view\n\n";
}
}
open CUSTID, ">$custid" or die "Couldnt open personal cart: $!\n";
print CUSTID "$itemno|$prod|$price|$desc|$quant|\n";
print CUSTID @custchoices;
close CUSTID or die "Couldnt close file: $!\n";
sleep(1);
print "Location: $url=view\n\n";
}
products.db looks like:
1002|One Dozen Yellow Roses|59.99|One dozen yellow roses with babys breath in a vase|
1003|One Dozen White Roses|59.99|Perfect for any occasion, one dozen white roses|
$custid is just a file with the contents of the persons shopping cart, which would store exactly as above but add one extra field for quantity
If any additional information is needed just let me know
Thanks,
Robert
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.