ok, i thought this would be overkill but below is what i have (i have changed some names to protect the innocent but it is all intact). the html::template works fine. and i'd prefer to keep two templates so that i can do some type of js on the results page to popup the search page (though i suppose i could do it with one and use an tmpl_if, doing them separate just seemed logical and would do slightly less processing).
also, i think you have a point of if not defining $_ so i'll change it (might be my issue). though i still think i'm off on my variable scopes...
you might have a point in defining my subs with my variables first, i just figured i'd go ahead and post the whole thing to confirm:
also, there is no need to have the sql connect and disconnect at the beginning and end, but it works for now so until i get this figured out, i figured i'd keep this as is. and there are major security issues with this right now that i'll take care of before i put this up. with those two points aside, i'll take any suggestions you have to make this work / simpler / better.
#!/usr/bin/perl -w
use strict;
use DBI;
use CGI qw( -no_undef_params :standard);
use HTML::Template;
my $param = "";
my ($ownsel, $sortby, $letter, $ascdesc, $t);
my $dbh = DBI->connect('DBI:mysql:dbname;host=localhost',
'someone', 'something')
or die "Database connection: $!";
my $cgi = CGI->new;
my $base = $cgi->url;
if ( $cgi->param ) {
if ( defined ( $cgi->param( 'sortby' ) ) ) {
$sortby = $_;
} else {
$sortby = "name";
}
if (defined ( $cgi->param( 'ascdesc' ) ) ) {
$ascdesc = $_;
} else {
$ascdesc = "desc";
}
if (defined ( $cgi->param( 'letter' ) ) ) {
$letter = $_;
} else {
$letter = " ";
}
ownertbl( $sortby, $ascdesc, $letter );
} else {
homepage();
}
sub homepage {
$t = HTML::Template->new( filename => 'home.tmpl',
global_vars => '1'
);
my @alphabet = ();
for ("A" .. "Z") {
my %letter;
$letter { 'letter' } = $_;
push(@alphabet, \%letter);
}
$t->param( alphabet => \@alphabet,
base => $base,
sortby => $sortby,
ascdesc => $ascdesc,
letter => $letter
);
print $cgi->header;
print $t->output;
}
sub ownertbl {
( $sortby, $ascdesc, $letter ) = @_;
my $sort = "";
$sort = "ORDER BY " . $sortby . $ascdesc;
if ( defined ( $letter ) ) {
$letter = "WHERE " . $sortby . " LIKE '" . $letter . "%'";
} else {
$letter = " ";
}
my $query = "SELECT field1, field2, field3, field4, field5
FROM table
$sort $letter";
my $sth = $dbh->prepare( $query );
$sth->execute( );
my @ownsel = ();
push @{$ownsel}, $_ while $_ = $sth->fetchrow_hashref();
$t = HTML::Template->new( filename => 'ownertbl.tmpl' );
$t->param(ownerall => \@ownsel,
base => $base,
letter => $letter
);
print STDERR "base = $base, sort = $sort, sortby = $sortby, ascdes
+c = $ascdesc, letter = $letter\n";
print $cgi->header;
print $t->output;
}
$dbh->disconnect;
|