#!/usr/bin/perl #************************************************************ #** Robert W. Arts ** #** Pikeville College ** #** 214 Sycamore Street** #** Pikeville, KY 41501** #** (606) 437-3417** #**Email: rarts@pc.edu** #************************************************************ # This Perl script is designed to accept a single input (text or numeric) # from a form, look for them in a database type flat file (comma delimited) , and # return a HTML page with the match (hopefully only one) in a table. # The script's design is to allow a student to input a designated code number # in order to retrieve their updated (defined by the instructor) course # grades. This script generates a HTML FORM, with itself as the "ACTION" # When this script is called it checks the STDIN for data it may # be passing to itself. If there is none, it generates the FORM # plus some help text on how to use it. If it does see search # keys in STDIN ($keys) it will again print the FORM, skip the # help text, open the data file, search for matches, and print # them as a HTML table. #****************************************************************** #******************** Subroutines ******************************** #****************************************************************** #****************************************************************** #**************** Get Passed data from form *********************** #****************************************************************** sub GET_STATE_INFO { read(STDIN, $save_string, $ENV{CONTENT_LENGTH}); # Yes- Use it @prompts = split(/&/,$save_string); foreach (@prompts) { ($tmp1, $tmp2) = split(/=/,$_); $tmp2 =~ s/\x2b/\x20/g; $tmp2 =~ s/%2C/\x2c/g; $tmp2 =~ s/%28/\x28/g; $tmp2 =~ s/%29/\x29/g; $tmp2 =~ s/%30/\x30/g; $tmp2 =~ s/%31/\x31/g; $tmp2 =~ s/%32/\x32/g; $tmp2 =~ s/%33/\x33/g; $tmp2 =~ s/%34/\x34/g; $tmp2 =~ s/%35/\x35/g; $tmp2 =~ s/%36/\x36/g; $tmp2 =~ s/%37/\x37/g; $tmp2 =~ s/%38/\x38/g; $fields{$tmp1}=$tmp2; } $keys = $fields{'keys'}; $search_type = $fields{'search_type'}; } #****************************************************************** #********** Search file for lines with all search keys ************ #****************************************************************** sub SEARCH_FOR_MATCH { @search_key = split(/\x20/,$keys); open(MYFILE,"106AGRD.txt") || print "

Error: Can't open grade book file<\H3>"; while() # Our Read loop { $in_line = $_; if ($search_type eq "and") { $found_flag = "Y"; } else { $found_flag = "N"; } foreach (@search_key) #check for each search key in the file line { if ( $in_line =~ m/$_/i) # Is key in the data line ? { if ($search_type eq "or") # Yes- { $found_flag = "Y"; # If its a OR search all we last; # need id one match } } else #No- { if ($search_type eq "and") # If its a AND search all we { # need is one non-match $found_flag = "N"; last; } } } #end of ForEach Loop if ( $found_flag eq "Y" ) { $kounter++; $in_line = $_; # split line in grade book data file into separate fields for printing in our table ($code, $lab1, $lab2, $lab3, $lab4, $lab5, $lab6, $lab7, $lab8, $lab9, $lab10, $lab11, $total ) = split(/,/,$in_line); print "", $code, "\n"; print "", $lab1, "\n"; print "", $lab2, "\n"; print "", $lab3, "\n"; print "", $lab4, "\n"; print "", $lab5, "\n"; print "", $lab6, "\n"; print "", $lab7, "\n"; print "", $lab8, "\n"; print "", $lab9, "\n"; print "", $lab10, "\n"; print "", $lab11, "\n"; print "", $total, "\n"; print "\n"; $kounter++; if ( $kounter > $max_hits) { last; } # break out of read loop } } # end of Read loop } #****************************************************************** #**************** Main Routine *********************** #****************************************************************** $max_hits = 1; #Set the maximum number of matches to be returned $kounter = 0; #Intialize counter that counts hits returned print("Content-Type: text/html\n\n"); print("Astronomy Lab - Section A - Course Grade Retrieval Form\n"); print("\n"); &GET_STATE_INFO; print "

Astronomy Lab - Section A - Current Course Grade Lookup

\n"; print "

Enter your personal code number in the box below. \n"; print "

\n"; print "\n"; #print "
Select Search type: \n"; $search_type = "and"; #print "\n AND"; #print "\n OR"; print "\n\n"; print "\n
\n"; if ( $keys eq "") { print ""; } else { print "
"; print "Your Current Course Grades Are:"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; &SEARCH_FOR_MATCH($keys); if ($kounter > $max_hits+1 ) { print "\n"; print "\n"; } print "
Code #Lab 1 %Lab 2 %Lab 3 %Lab 4 %Lab 5 %Lab 6 %Lab 7 %Lab 8 %Lab 9 %Lab 10 %Lab 11 %Total %
Limit of one match exceeded. Please contact instructor.
\n"; } print("\n"); print("\n");