View Full Version : Limiting sale item amounts
Anarchist
03-02-2005, 02:31 AM
I am using a script that takes input from a form and writes the values to a database these are sale items.
What I want to do is limit the amount of items for sale, so I cannot over sell any stock.
Say I have:
CD's * 60
DVD's * 30
DVD'rs* 60
I wish to dissallow the sale of more than I have available.
I have had a thought on how to achieve this but the code is a mess, I think the idea is valid though.
Could someone help me on this plz?
#!/usr/bin/perl
if @items = CD
open(COUNT, "<CD.txt");
$num = <COUNT>;
close (COUNT);
$num++;
open (COUNT, ">CD.txt");
print COUNT $num;
close (COUNT);
$num=sprintf("%05d",$num);
if $num >30;
print "Content-type: text/html\n\n";
print "<html><h1>Sold Out</h1></html>\n";
mlseim
03-02-2005, 04:45 AM
I'm a bit confused ...
When you sell an item, don't you subtract the amount sold
from an amount in stock ... so you would be checking to
see if the amount in stock is zero or less? When you re-stock
items, you add the numbers back into the file?
You should give us an example of your actual database.
You said you already have a form that writes something into
a database? :confused:
Anarchist
03-02-2005, 12:21 PM
When you sell an item, don't you subtract the amount sold
from an amount in stock ... so you would be checking to
see if the amount in stock is zero or less? When you re-stock
items, you add the numbers back into the file?
Well that script counts the number of times [or is supposed to]the item has been sold, it counts upwards. What I was trying to do was set a limit on the number so when say 3 sales on a certain item was reached [sold out] about 2/3 of that script works it will count upwards but wil not output sold out on the set level.
All I am trying to do is monitor the sale items as their numbers are limited. I really want a way to monitor all of them @ once not as in my example, it is trying to check CD's only.
You don't need to see the database as this script comes in before anything is written or read from it. The script example will write a txt file which keeps count of the number of times the item has been sold or is supposed to. :)
This works, only it wont stop on the set amount.
#!/usr/bin/perl
open(COUNT, "<CD.txt");
$num = <COUNT>;
close (COUNT);
$num++;
open (COUNT, ">CD.txt");
print COUNT $num;
close (COUNT);
$num=sprintf("%05d",$num);
print "Content-type: text/html\n\n";
print "<html><h1>You have sold $num</h1></html>\n";
mlseim
03-02-2005, 02:48 PM
Version 1 ... displays message, but keeps on adding to database
==================================================
#!/usr/bin/perl
open(COUNT, "<CD.txt");
$num = <COUNT>;
close (COUNT);
$num++;
open (COUNT, ">CD.txt");
print COUNT $num;
close (COUNT);
$num=sprintf("%05d",$num);
$mess1="You have sold $num";
$cd_limit = "5"; # What the limit is set for.
if($num >= $cd_limit){$mess1="CD's Are All Sold Out!";}
print "Content-type: text/html\n\n";
print "<html><h1>$mess1</h1></html>\n";
Version 2 ... STOP updating the file if the limit is reached.
==================================================
#!/usr/bin/perl
open(COUNT, "<CD.txt");
$num = <COUNT>;
close (COUNT);
$numf=sprintf("%05d",$num+1);
$mess1="You have sold $numf";
$cd_limit = "5"; # What the limit is set for.
$num++;
unless($num > $cd_limit){
open (COUNT, ">CD.txt");
print COUNT $num;
close (COUNT);
}
if($num >= $cd_limit){$mess1="CD's Are All Sold Out!";}
print "Content-type: text/html\n\n";
print "<html><h1>$mess1</h1></html>\n";
#
Anarchist
03-02-2005, 04:57 PM
Thanks,
I am looking to go through all the sale items though, can this be done? I tried this but no go
#!/usr/bin/perl
if (@items=CD) {
open(COUNT, "<CD.txt");
$num = <COUNT>;
close (COUNT);
$numf=sprintf("%05d",$num+1);
$mess1="You have sold $numf";
$cd_limit = "50"; # What the limit is set for.
$num++;
unless($num > $cd_limit){
open (COUNT, ">CD.txt");
print COUNT $num;
close (COUNT);
}
if($num >= $cd_limit){$mess1="CD's Are All Sold Out!";}
print "Content-type: text/html\n\n";
print "<html><h1>$mess1</h1></html>\n";
} else {
} if (@items=DVD) {
open(COUNT, "<DVD.txt");
$num = <COUNT>;
close (COUNT);
$numf=sprintf("%05d",$num+1);
$mess2="You have sold $numf";
$dvd_limit = "50"; # What the limit is set for.
$num++;
unless($num > $dvd_limit){
open (COUNT, ">DVD.txt");
print COUNT $num;
close (COUNT);
}
if($num >= $dvd_limit){$mess2="DVD's Are All Sold Out!";}
print "Content-type: text/html\n\n";
print "<html><h1>$mess2</h1></html>\n";
I also wanna lose the [you have sold#### message] and just keep the [sold out]. I admit I am lost on this.
Anarchist
mlseim
03-02-2005, 05:41 PM
Somewhere along the line, you must have a part in your script that
takes the database and transfers it to the array ...
We need to know what the database entries look like.
How do you know it's a CD and not a DVD?
There must be a field or element in the database that
has that info. We need to see where that is to determine
which one will be looked at.
You can't just go like this: if (@items=CD)
Copy and paste part of the database so we can see a few of the lines.
------------------
For the "All Sold Out" thing ... just change the text to what you want:
if($num >= $cd_limit){$mess1="CD's Are All Sold Out!";}
.
Anarchist
03-02-2005, 06:52 PM
The script gets the items from a form:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sale Items</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
/*<![CDATA[*/
<!--
/*<![CDATA[*/
/*]]>*/
body {
background-color: #FDFDFD;
}
/*]]>*/
/*<![CDATA[*/
div.c10 {
text-align: right;
}
div.c11 {
text-align: center;
}
div.c4 {
text-align: center;
}
div.c8 {
text-align: right;
}
table.c1 {
width: 49%;
}
table.c7 {
width: 410px;
}
td.c3 {
width: 34%;
}
td.c6 {
width: 32%;
}
tr.c2 {
background-color: #CCCCCC;
}
tr.c9 {
background-color: #999999;
}
.c5 {
font-size: normal;
}
-->
/*]]>*/
</style>
</head>
<body>
<div class="c4">
<p><br />
</p>
<form action="order4.cgi" method="post" name="theform" id="theform">
<table border="0" cellpadding="2" class="c7">
<tr bordercolor="#000000" class="c9">
<td><div class="c10"><em><strong>How many items:</strong></em> </div></td>
<td><input name="amount" type="text" value="1" size="5" maxlength="2" /> </td>
</tr>
<tr bordercolor="#000000" class="c9">
<td> <div class="c8"><em><span class="c5"><strong>Select item
:</strong></span></em> </div></td>
<td> <select name="items">
<option selected="selected" value="">Please select </option>
<option value="Compact Discs">Compact Discs</option>
<option value="DVD's">DVD's </option>
<option value="CD/R's">CD/R's </option>
<option value="CD/RW">CD/RW </option>
</select></td>
</tr>
<tr bordercolor="#000000" class="c9">
<td> <div class="c10"><em><strong>Select delivery method :</strong></em></div></td>
<td> <select name="type">
<option selected="selected" value=""> Please select </option>
<option value="Collect">Collect </option>
<option value="Post UK">Post UK</option>
<option value="Post Worldwide">Post Worldwide </option>
</select> </td>
</tr>
<tr bordercolor="#000000" class="c9">
<td colspan="2"> <div class="c11">
<input type="Submit" value="submit"/>
<input type="Reset" value="Reset"/>
</div></td>
</tr>
</table>
</form>
</div>
</body>
</html>
This is the begining of the script
#!/www/perl/bin/perl.exe
use CGI;
use DBI;
#parse the form arguments into variables to hold data
#get delivery option from form and hold values in variable
$type=CGI::param("type");
#get item choice from form and hold values in variable
@items=CGI::param("items");
#get amount needed
$amount=CGI::param("amount");
#pass time to a variable for use later
$lt=localtime;
#set costs to 0 prior to calculation
$typecost=0;
$itemcost=0;
$total=0;
@inPrices;
%price;
For the "All Sold Out" thing ... just change the text to what you want:
if($num >= $cd_limit){$mess1="CD's Are All Sold Out!";}
I am confused now, what I meant was I want to not show in print each time an item is sold, the numeric count like so 00055, I only want HTML output when the item requested is sold out.
Can you see wha I mean by that?
mlseim
03-02-2005, 07:37 PM
OK, so I can see that there's more to this ...
I changed the form so that the option values are the same as
the filename that stores the count. Example: CD.txt
Here's the form part:
========================================================
<form action="/cgi-bin/order4.cgi" method="post" name="theform" id="theform">
<table border="0" cellpadding="2" class="c7">
<tr bordercolor="#000000" class="c9">
<td><div class="c10"><em><strong>How many items:</strong></em> </div></td>
<td><input name="amount" type="text" value="1" size="5" maxlength="2" /> </td>
</tr>
<tr bordercolor="#000000" class="c9">
<td> <div class="c8"><em><span class="c5"><strong>Select item
:</strong></span></em> </div></td>
<td> <select name="items">
<option selected="selected" value="">Please select </option>
<option value="CD">Compact Discs</option>
<option value="DVD">DVD's </option>
<option value="CDR">CD/R's </option>
<option value="CDRW">CD/RW </option>
</select></td>
</tr>
<tr bordercolor="#000000" class="c9">
<td> <div class="c10"><em><strong>Select delivery method :</strong></em></div></td>
<td> <select name="type">
<option selected="selected" value=""> Please select </option>
<option value="Collect">Collect </option>
<option value="Post UK">Post UK</option>
<option value="Post Worldwide">Post Worldwide </option>
</select> </td>
</tr>
<tr bordercolor="#000000" class="c9">
<td colspan="2"> <div class="c11">
<input type="Submit" value="submit"/>
<input type="Reset" value="Reset"/>
</div></td>
</tr>
</table>
</form>
========================================================
Here's the Perl script that takes in the values from the form and
checks the appropriate text file ... assuming you have a file made
for each thing (CD.txt DVD.txt CDR.txt CDRW.txt)
It only prints if it's out, otherwise it prints nothing.
You didn't say what to do if it prints nothing ... does it go to another
webpage?
Here is what I have so far ... I can't test with the actual database, since
I don't have it installed....
========================================================
#!/usr/bin/perl
use CGI;
use DBI;
#parse the form arguments into variables to hold data
#get delivery option from form and hold values in variable
$type=CGI::param("type");
#get item choice from form and hold values in variable
$items=CGI::param("items");
#get amount needed
$amount=CGI::param("amount");
#pass time to a variable for use later
$lt=localtime;
#set costs to 0 prior to calculation
$typecost=0;
$itemcost=0;
$total=0;
@inPrices;
%price;
# Notice that the name of the options on the form is the same
# name as the text file ... "CD.txt" for CD's
open(COUNT, "<$items.txt");
$num = <COUNT>;
close (COUNT);
$num = $num + $amount;
open (COUNT, ">$items.txt");
print COUNT $num;
close (COUNT);
$num=sprintf("%05d",$num);
if($items eq "CD"){$limit = 10;}
if($items eq "DVD"){$limit = 15;}
if($items eq "CDR"){$limit = 25;}
if($items eq "CDRW"){$limit = 35;}
if($num >= $limit){$mess1="Sold Out!";}
print "Content-type: text/html\n\n";
print "<html><h1>$mess1</h1></html>\n";
#
Anarchist
03-02-2005, 07:40 PM
I'll check it out, did you see the attachment?
mlseim
03-02-2005, 07:43 PM
I see that attachment now ....
You are modifiying an existing script that handles ticket and seating?
To use the same database for your CD's?
Anarchist
03-02-2005, 07:47 PM
Same script different values same method.It works! HeHe :thumbsup:
That post you made earlier almost does the job just fine, but when an item is sold out I would like the script to stop processing, at the moment it carries on and outputs the final confirmation etc.
mlseim
03-02-2005, 07:57 PM
Maybe it would be best if you reposted the script (zipped) ...
as you have it now, almost working. Your latest changes to it.
I think the zipped file above doesn't have any of your changes in it.
Anarchist
03-02-2005, 08:03 PM
Hope you can get this to produce, if not i'll have to run it as is...to hell with sale items.
mlseim
03-02-2005, 08:21 PM
OK ... here's the next version.
I renamed it so you don't accidently overwrite your original.
See attachment....
Anarchist
03-02-2005, 09:33 PM
It is better, but whatever amount and whatever item you choose [sold out] I bet you are as annoyed by this as I am by now.
You must have a lot of patience. :D
mlseim
03-02-2005, 09:43 PM
Yes it does takes patience ... but I am persistant ...
And you do have the 4 text files as:
CD.txt
DVD.txt
CDR.txt
CDRW.txt
These have quantity values in them that
are less than the $limits set?
... and add this line:
$limit = 9999;
if($item eq "CD"){$limit = 10;}
if($item eq "DVD"){$limit = 15;}
if($item eq "CDR"){$limit = 25;}
if($item eq "CDRW"){$limit = 35;}
Check those and see because if you ran this a lot
of times, you might have exceeded the $limit values.
And also, you changed the OPTION values on the form?
I assume you did that already.
<option value="CD">Compact Discs</option>
<option value="DVD">DVD's </option>
<option value="CDR">CD/R's </option>
<option value="CDRW">CD/RW </option>
I sort of tested it (without the database) and it works.
Anarchist
03-02-2005, 09:57 PM
Thankyou for your patience, it is now working, and in the future I can port it to other scripts as and when I need it :thumbsup:
It was the option values BTW.
mlseim
03-02-2005, 10:27 PM
With all of your frustration, I hope you took away something
important. You learned some basic Perl programming ...
If you analyze the script(s) and see how the instructions
are arranged and processed, you'll begin to really learn how
to do other things in the future. :thumbsup:
Anarchist
03-03-2005, 12:14 AM
I have hit another brick wall, I put more code into the script to detect if the user asks for more than are available, the script appears to work fine but the high amount requested by the user is still written to the .txt file.
Therefore the items appear to be sold out which is an invalid statement, this is because the rejected transaction is written to file.
If you feel like looking do so, if you dont I wont blame you , you helped a ton already :)
mlseim
03-03-2005, 04:20 AM
OK, give this one a shot ... I "winged" it, without testing it.
EDIT: After uploading, I just thought of something else.
If they order less than 1, you should print out another error message:
Do this right after -- %price;
%price;
if($amount < 1){
print <<PAGE;
Content-type: text/html \n\n
<html>
.
. insert all the same tags here....
. ... you must enter at least one item
.
PAGE
exit 0;
}
.
Anarchist
03-03-2005, 10:45 AM
No go sorry, the message output is a constant amount of 0 items available in every option.
This one attached would be perfect IF it would NOT write amounts to .txt when the order is invalid eg: if too many are requested
mlseim
03-03-2005, 02:03 PM
Try this ...
I see you changed the topic of the program.
I assume the OPTIONS fields on your form match the one's on the script?
Anarchist
03-03-2005, 06:06 PM
It's exactly the same script mate, just different variable names, and different form. ;)
I will study the latest file, I am sure there must be a simple way to stop the excessive number being passed to the .txt.
If @seating > $limit then
Output message "please pick a smaller amount please"
Exit
0;
Something like that.
Anarchist
Anarchist
03-03-2005, 06:13 PM
I am going to give it some intensive checking but I think you have hit the jackpot this time.
How did you get around the problem?
Congratulations and many many thanks :cool:
Anarchist
03-04-2005, 12:40 PM
It's developed another bug, sorry :( say I have front stalls limit @10 If I enter 6 requested tickets then 6 again I get the "sold out".
The txt file written reads 6 so the script is reading the amount wanted comparing to the amount left for sale and saying sold out instead of saying there are not that many available.
So sorry to hassle you mate. :thumbsup:
mlseim
03-04-2005, 02:15 PM
No problem ... we'll get it right.
Anarchist
03-04-2005, 02:39 PM
This time for real, it is working.
Brilliant :D
Anarchist
03-08-2005, 09:13 PM
:thumbsup: Sorted IT !
:eek:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.