CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Course Registration w/counter

 



scud
Deleted

Sep 2, 2000, 3:49 AM

Post #1 of 2 (849 views)
Course Registration w/counter Can't Post

I am looking for a generic course registration form that allows me to specify a course limit for each course and when the limit is met, disable that course from being selected (or surpress selection from the form).

For example, if I have three courses available, each with 10 seats, I'd like the form to disable a course choice once the tenth person enrolls.

Extra credit if the form tells me how many are already enrolled before I enroll!


TheGame+
Deleted

Sep 2, 2000, 9:28 AM

Post #2 of 2 (849 views)
Re: Course Registration w/counter [In reply to] Can't Post

Here's a little something to help you on the way. I'll leave the improvements, customisation etc. to you Smile

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -w
#
# Simple course registration script
#
# TheGame, 02 September 2000
# (jointhegame@crosswinds.net)
#
# The courses.txt file should have the following format :
# ID<tab>Title<tab>Limit<tab>Count
# ID<tab>Title<tab>Limit<tab>Count
# ...
#
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use Fcntl qw(:flock);
use strict;

# keep these in a directory that isn't accessible from the web
my $filepath = '/path/to/my/files'; # note : no final / here
my $coursefile = 'courses.txt';

# print HTTP headers and start HTML page
print header,start_html('Course Registration');

# check if we're submitting a registration
if (param('course')) {
# declare some variables
my $found = 0;
my @lines = ();
my $regfile = '';

# grab the registration details
my $course = param('course');
my $name = param('name');
my $whatever = param('whatever');

# allow only "plain ASCII" chars here
$name =~ tr/\040-\176//cd;
$whatever =~ tr/\040-\176//cd;

# open the coursefile and increase the count
open(FILE,"+<$filepath/$coursefile") | | die("Can't open course file - $!\n");
flock(FILE, LOCK_EX) | | die("Can't lock course file - $!\n");
while (<FILE> ) {
chomp;
my ($id,$title,$limit,$count) = split(/\t/);
if ($id eq $course) {
$count++;
if ($count > $limit) {
$found = 2; # too many
}
else {
$found = 1; # OK
$regfile = "$id.txt";
}
}
push(@lines,"$id\t$title\t$limit\t$count");
}
if ($found == 1) {
# update the course file
seek(FILE,0,0);
truncate(FILE,0) | | die("Can't truncate course file - $!\n");
foreach my $line (@lines) {
print FILE "$line\n";
}
close(FILE);

# save the registration details in a separate file
open(FILE,">>$filepath/$regfile") | | die("Can't open registration file - $!\n");
print FILE "$name\t$whatever\n";
close(FILE);

print <<EOT;
<H2>Registered</H2>
Thanks for your registration
EOT
}
elsif ($found == 2) {
close(FILE);
print <<EOT;
<H2>Too Late</H2>
Sorry, someone else has already taken that course while you were filling in
the registration form.
EOT
}
else {
close(FILE);
print <<EOT;
<H2>Unknown Course</H2>
You seem to have chosen a course that isn't available right now...<BR>
Please try again.
EOT
}
}
else {
# declare some variables
my $courselist = '';
my $courseselect = '';

# open the coursefile, fill in the courselist and the courseselect
open(FILE,"+<$filepath/$coursefile") | | die("Can't open course file - $!\n");
flock(FILE, LOCK_EX) | | die("Can't lock course file - $!\n");
while (<FILE> ) {
chomp;
my ($id,$title,$limit,$count) = split(/\t/);
$count | |= '-';
$courselist .= qq(\t<TR><TD>$id</TD><TD>$title</TD><TD>$limit</TD><TD>$count</TD></TR>\n);
if ($count < $limit) {
$courseselect .= qq(\t\t\t<OPTION VALUE="$id">$title\n);
}
}
close(FILE);

# show the list of courses and registration form
print <<EOT;
<H2>List of Courses</H2>
<TABLE BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR>
<TH>ID</TH><TH>Title</TH><TH>Limit</TH><TH>Count</TH>
</TR>
$courselist
</TABLE>
<P>
<H2>Register Now</H2>
<FORM METHOD="POST">
<TABLE>
<TR>
<TD>Course :</TD>
<TD>
<SELECT NAME="course">
$courseselect
</SELECT>
</TD>
</TR>
<TR>
<TD>Name :</TD>
<TD><INPUT TYPE="text" NAME="name"></TD>
</TR>
<TR>
<TD>Whatever :</TD>
<TD><INPUT TYPE="text" NAME="whatever"></TD>
</TR>
<TR>
<TD> </TD>
<TD><INPUT TYPE="submit"></TD>
</TR>
</TABLE>
</FORM>
EOT
}

# end HTML page
print end_html;

# done
exit;
</pre><HR></BLOCKQUOTE>


[This message has been edited by TheGame+ (edited 09-02-2000).]

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives