Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Question about properly laying out a database

by Stamp_Guy (Monk)
on Dec 13, 2001 at 07:22 UTC ( [id://131524]=note: print w/replies, xml ) Need Help??


in reply to Question about properly laying out a database

I've kinda got a mix of you guy's suggestions. This is totally a test script here. I'll be using HTML::Template for all the html and will be cleaning up a few things, but to give you a general idea, here's what I made. It seems to work perfectly for my purposes. I tested it with as many as 3000 entries, even though it isn't likely that it will EVER have more than 500 (300 is what we expect). Comments are welcome!

#!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 "<html><head><title>Output Page</title><style type=\"text/cs +s\"><!-- .header { font: bold 10pt verdana; color: white; background: + orange; } .caption { font: 8pt verdana; color:black; } --></style></ +head><body><TABLE width=600 cellpadding=0 cellspacing=0>"; my %OUTPUT=(); my $counter = 0; tie (my %INDEX, "DB_File", "$OPTIONS{databaseDir}/index.db", O_RDW +R|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 "<td>No cars matched your search. Please try again.</td +>"; } else { print "<tr><td colspan=4 align=right class=caption>Click on th +e header to sort by that category</td></tr>"; print "<tr> <td class=\"header\"><a href=\"carsearch.cgi?sortBy=Make\& +make=$make\&model=$model\&price=$price\&year=$year\">Make</a></td> <td class=\"header\"><a href=\"carsearch.cgi?sortBy=Model\ +&make=$make\&model=$model\&price=$price\&year=$year\">Model</a></td> <td class=\"header\"><a href=\"carsearch.cgi?sortBy=Year\& +make=$make\&model=$model\&price=$price\&year=$year\">Year</a></td> <td class=\"header\"><a href=\"carsearch.cgi?sortBy=Price\ +&make=$make\&model=$model\&price=$price\&year=$year\">Price</a></td> </tr>"; foreach my $key (sort { my @a = split(/\|/, $OUTPUT{$a}); my @ +b = split(/\|/, $OUTPUT{$b}); $a[$sortBy{$sortBy}] cmp $b[$sortBy{$so +rtBy}]; } (keys %OUTPUT)) { my @details = split(/\|/, $OUTPUT{$key}); print "<tr><td><a href=\"carsearch.cgi?action=display\&id= +$key\">$details[0]</a></td>"; print "<td>$details[1]</td>"; print "<td>$details[3]"; print " <td>\$$details[2]</td></tr>\n "; } } print "</TABLE></body></html>"; untie (%DATABASE); untie (%INDEX); } sub display { tie (my %INDEX, "DB_File", "$OPTIONS{databaseDir}/index.db", O_RDW +R|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]}<br>"; print "Model: $models{$key[1]}<br>"; print "Year: $details[0]<br>"; print "Price: \$$key[3]<p>"; print "Description: $details[1]"; untie (%DATABASE); untie (%INDEX); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://131524]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 05:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found