#!c:/perl/bin/perl.exe -w use strict; use CGI::Carp qw(fatalsToBrowser); ##################################################################### # CONFIGURATION ##################################################################### my %OPTIONS = ( databaseDir => "c:/", ); my %makes = ( 1 => 'all', 2 => 'Chevrolet', 3 => 'Ford', 4 => 'Pontiac', 5 => 'Honda', ); my %models = ( 1 => 'all', 2 => 'Car', 3 => 'Truck', 4 => 'Van', 5 => 'Mini Van', ); my %years = ( 1 => 'all', 2 => '1980-1985', 3 => '1986-1990', 4 => '1991-1995', 5 => '1996-2000', 6 => '2000-Present', ); my %sortBy = ( # The numbers here correspond to the place in the output hash string. Make => '0', Model => '1', Price => '2', Year => '3', ); ##################################################################### # END OF CONFIGURATION ##################################################################### use DB_File; use CGI; my $query = new CGI; my $action = $query->param('action'); my $id = $query->param('id'); my $sortBy = $query->param('sortBy'); my $make = $query->param('make'); my $model = $query->param('model'); my $year = $query->param('year'); my $price = $query->param('price'); my %OUTPUT; print $query->header; my %ACTIONS = ( search => \&search, display => \&display, ); if ($action) { # If an action is given, active the appropriate subroutine $ACTIONS{$action}->(); } else { # If no action is given, display the update page $ACTIONS{'search'}->(); } ##################################################################### # SUBROUTINES ##################################################################### sub check { if($_[0] == 1) { return(1); } elsif($_[0] == $_[1]) { return(1); } else { return(0); } } sub checkPrice { if($_[0] <= $price) { return(1); } else { return(0); } } sub search { print "Output Page"; my %OUTPUT=(); my $counter = 0; tie (my %INDEX, "DB_File", "$OPTIONS{databaseDir}/index.db", O_RDWR|O_CREAT, 0755, $DB_HASH) || die "Cannot open database: $!\n"; tie (my %DATABASE, "DB_File", "$OPTIONS{databaseDir}/database.db", O_RDWR|O_CREAT, 0755, $DB_HASH) || die "Cannot open database: $!\n"; foreach my $key (keys %INDEX) { my @key = split(/\|/, $INDEX{$key}); if(&check("$make","$key[0]")) { if(&check("$model","$key[1]")) { if(&check("$year","$key[2]")) { if(&checkPrice("$key[3]")) { # Form of output string: Make | Model | Price | Details (from database. in this case, year|description) $OUTPUT{$key} = "$makes{$key[0]}|$models{$key[1]}|$key[3]|$DATABASE{$key}"; $counter++; } } } } } if($counter == 0) { print ""; } else { print ""; print ""; foreach my $key (sort { my @a = split(/\|/, $OUTPUT{$a}); my @b = split(/\|/, $OUTPUT{$b}); $a[$sortBy{$sortBy}] cmp $b[$sortBy{$sortBy}]; } (keys %OUTPUT)) { my @details = split(/\|/, $OUTPUT{$key}); print ""; print ""; print "\n "; } } print "
No cars matched your search. Please try again.
Click on the header to sort by that category
Make Model Year Price
$details[0]$details[1]$details[3]"; print " \$$details[2]
"; untie (%DATABASE); untie (%INDEX); } sub display { tie (my %INDEX, "DB_File", "$OPTIONS{databaseDir}/index.db", O_RDWR|O_CREAT, 0755, $DB_HASH) || die "Cannot open database: $!\n"; tie (my %DATABASE, "DB_File", "$OPTIONS{databaseDir}/database.db", O_RDWR|O_CREAT, 0755, $DB_HASH) || die "Cannot open database: $!\n"; my @key = split(/\|/, $INDEX{$id}); my @details = split(/\|/, $DATABASE{$id}); print "Make: $makes{$key[0]}
"; print "Model: $models{$key[1]}
"; print "Year: $details[0]
"; print "Price: \$$key[3]

"; print "Description: $details[1]"; untie (%DATABASE); untie (%INDEX); }