#!/usr/local/bin/perl # program: search.cgi # author: Chris Wallace # date: March 1998 # purpose: to display those wines which match the search criteria # a wine satisfies the criteria if it matches ANY of the criteria # i.e. the criteria are ORed # input - CGI form containing search fields # wine.dat the data file of wines # output - HTML document containing a turnround form and the # records which satisfied the search criteria # Change history # # 5 March - some documentatation added # one minor error corrected ( single & in condition) # 8 March - quoting of data in HTML required # bad tag in output # 10 march - converted to use DBM file # use CGI qw(:standard); &getSearchFields; &validateSearchFields; &outputHTMLHead; if (!$error) { &search; } &outputHTMLfoot; #---------------------------------------------------------------- sub search { dbmopen(%wines, "winedb", 0444) or die "Cant open wine db\n"; print "\n"; foreach $ref (keys(%wines)) { $found=1; $rec = $wines{$ref}; &getFields($rec); if ($Sref) { $found = $found && ($Sref eq $ref);} if ($Sname) {$found = $found && ($name =~/$Sname/i);} if ($Sdescription) {$found = $found && ($description =~/$Sdescription/i);} if ($Ssweetness && $sweetness) {$found = $found && ($Ssweetness eq $sweetness);} if ($Sfullness && $fullness) {$found = $found && ($Sfullness eq $fullness); } if ($SbottlePrice) {$found = $found && ($SbottlePrice >= $bottlePrice);} if ($Svintage && $vintage) {$found = $found && ( $Svintage >= $vintage );} if ($found) { &writeRecord;} } print "
\n"; dbmclose(%wines); } #----------------------------------------------------------------- sub getSearchFields { $Sref=param('ref'); &trim($Sref); $Sname=param('name'); &trim($Sname); $Sdescription=param('description'); &trim($Sdescription); $Ssweetness=param('sweetness'); &trim($Ssweetness); $Sfullness=param('fullness'); &trim($Sfullness); $SbottlePrice=param('bottlePrice'); &trim($SbottlePrice); $Svintage=param('vintage'); &trim($Svintage); } #-------------------------------------------------------------------- sub trim { s/^\s*//; s/\s*$//; } #------------------------------------------------------------------ sub validateSearchFields { if ($Sref && $Sref !~/^[A-Z][A-Z]\d+$/) {$Mref = 'invalid';} if ($Ssweetness && $Ssweetness !~/^[1-7]$/) {$Msweetness = 'invalid';} if ($Sfullness && $Sfullness !~/^[ABC]$/) {$Mfullness = 'invalid';} if ($bottlePrice && $SbottlePrice !~/^\d+\.\d\d$/) {$MbottlePrice = 'invalid';} if ($Svintage && $Svintage !~/^\d\d\d\d$/) {$Mvintage = 'invalid';} $error =$Mref.$Msweetness.$Mfullness.$MbottlePrice.$Mvintage; } #------------------------------------------------------------------------- sub getFields { local($r) = $_[0]; chomp($r); ($ref,$name,$description,$sweetness,$fullness,$bottlePrice,$casePrice,$vintage,$newWine,$newVintage) = split(/\t/, $r); } #-------------------------------------------------------------------------- sub writeRecord { if ($newWine) { $Oname="$name"; }else { $Oname=$name; } print <$ref$sweetness $fullness$Oname $description$bottlePrice$casePrice END } #-------------------------------------------------------------------- sub outputHTMLHead { print <Wine Society - Search Form

Wine Society - Search Form

HelpAdd new Wine
Reference number $Mref
Name includes $Mname
Description includes $Mdescription
Sweetness [1..7 or blank] $Msweetness
Fullness [A-C or blank] $Mfullness
Bottle Price less than $MbottlePrice
Vintage $Mvintage
END } sub outputHTMLfoot { print < END }