Helllo,
I have some questions here about my code. (which is going to have some issues)
I am going to send this as 2 emails so that you don't have to scroll endlessly :)
Goal: display database in a table with checkboxes so that the user can delete
selected messages
Please let me know how I can Improve my script and if you see anything that is
bad style/practice.
Thanks,
Dave Gilden
------------- The display script ----
#!/usr/bin/perl -w
#display_guest.cgi
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use strict;
my $user_name = "####";
my $user_password = "####";
my $sql_server = "#####";
my $table_name = "guestbook";
my $qs = $ENV{'QUERY_STRING'};
my $secret_word = '###';
if ($qs !~ /$secret_word/) {
print header;
print "Your not authorized to make changes!\n";
exit;
}
my @colors = ('lightgrey', 'white');
my ($title,$sth,$dbh,$sql,$id,$maxEntries,%sort_direction,$offset,$sort_direction,
$name,$email,$datecreated,$comments);
&initialize_dbi;
#############################
# get form data
############################
$sort_direction = param('display_direction');
$offset = param('offset');
# Get how many records there are total!
run_statement("select * from guestbook;");
$maxEntries= $sth->rows;
init(lc(param('action')));
$sql = "select * from $table_name order by id limit " . $offset . ",10;";
run_statement($sql);
my @data_out;
my $comments_num = $offset;
my $firstVal = $comments_num;
while (($id,$datecreated,$email,$name,$comments) = $sth->fetchrow) {
$comments_num++;
push(@data_out, "<tr>\n<td>" . checkbox( -name =>'id', -value =>$id, -checked =>0,
-label=>'') .
"</td><td>$datecreated</td><td>$email</td><td>$name</td><td>$comments</td></tr>\n");
}
$dbh->disconnect; # close date base connection
$title = "Guestbook entries $firstVal - $comments_num";
#######################
# Start HTML OUT
#######################
print header;
print qq|<meta http-equiv="Pragma" content="no-cache">\n|;
print start_html( -title => $title );
print "\n", h2($title);
print start_form( -action => "./display_guestbook.cgi?$secret_word" , -method =>
'POST' ), "\n";
#############################
# set hidden fields
#############################
param('offset', $offset);
print hidden(-name => 'offset'), "\n<p>",
######################
# Buttons
######################
submit( -name=> 'action', -value => 'Sort Display Order' ), " \n";
%sort_direction = (
1 => 'First to Last ',
0 => 'Last to First ',
);
print radio_group(
-name => 'display_direction',
-values => [ keys %sort_direction ],
-default => $sort_direction,
-labels => \%sort_direction
),
"</p>\n";
print " \n"; # space out the buttons
print submit( -name => 'action', -value => 'Previous 10 Entries' ) if $offset > 0;
print " \n"; # space out the buttons
print submit( -name => 'action', -value => 'Next 10 Entries' ) unless $comments_num
== $maxEntries;
print "\n", end_form, "\n";
print "<br> <br>\n";
print start_form( action => "update_guestbook.cgi" , -method => 'POST' ), "\n";
print submit( -name => 'delete', -value => 'Delete Selected' );
# Start Table Data
print <<TABLEHEADER;
<br><table border="1" cellspacing="4" cellpadding="0" width="98%">
<tr>
<th style="color:red;">Delete</th><th>Date
Created</th><th>Email</th><th>Name</th><th>Comments</th>
</tr>
TABLEHEADER
# print $data_out;
# Sort Direction for numbers
# set up $comments_num to count up or down
# $comments_num = ($sort_direction) ? 0 : 10;
while (@data_out){
my $tmp = ($sort_direction) ? (shift @data_out) : (pop @data_out);
print $tmp;
}
# End Table Rows
print "</table>\n";
print "\n", end_form, "\n" ,end_html;
print "<!--\$sort_direction: $sort_direction -->\n";
exit;
##############################
# SUBS
##############################
my $path_to_cgi = "http://#######/";
sub init{
my $act = shift;
if ($act =~ /^sort/){
redirect("$path_to_cgi/guestbook.cgi");
} elsif ($act =~ m/^next/){
$offset += 10;
} elsif ($act =~ m/^previous/) {
$offset = $offset - 10;
} else {
$offset =0;
$comments_num =0;
$sort_direction =0;
}
}
sub initialize_dbi{
my $drh = DBI->install_driver( 'mysql' );
$dbh = DBI->connect("DBI:mysql:$user_name:$sql_server", $user_name, $user_password);
die "Cannot connect: $DBI::errstr\n" unless $ ;
}
sub run_statement{
my $stmt = "$_[0]";
$sth = $dbh->prepare($stmt);
$sth->execute;
}
__END__
==============================================
Cora Connection: Your West African Music Source
Resources, Recordings, Instruments & More!
<http://www.coraconnection.com/>
==============================================
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]