T
Tom
Hello.
I am learning Perl and have decided to create a web page that has
dropdowns populated by a postgreSQL DB. I've been able to do this and
the page looks fine but the code is awful. A chunk of the code is
posted below; can anyone suggest a way to clean this up and make it
more efficient? Namely, I am trying to consolidate the actual
population of the <SELECT> into one loop, and figure out a way to get
rid of the ugly "$i < scalar(@array)" line.
This all works, but I'm still learning Perl and haven't figured out
how to make it work good!
Thanks,
Tom
CODE BELOW:
===========
#!/opt/perl/bin/perl -wT
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
my $dbh = DBI->connect( "dbig:dbname=web" ) || die;
my @a = ('groupdescription','groups');
my @b = ('role','servers_role');
my $sth1 = $dbh->prepare("Select $a[0] from $a[1] order by $a[0]");
my $sth2 = $dbh->prepare("Select $b[0] from $b[1] order by $b[0]");
$sth1->execute;
$sth2->execute;
print $q->header,
$q->start_html,
$q->startform,
"<table><Tr><td>$a[0]</td>",
"<td><Select name=\"$a[0]\">";
while( @row1 = $sth1->fetchrow ) {
for( $i = 0; $i < scalar(@row1); $i++ ) {
print "<option value=\"$row1[$i]\">$row1[$i]";
}
}
print "</Select></td></Tr>",
"<Tr><td>$b[0]</td>",
"<td><Select name=\"$b[0]\">";
while( @row2 = $sth2->fetchrow ) {
for( $i = 0; $i < scalar(@row2); $i++ ) {
print "<option value=\"$row2[$i]\">$row2[$i]";
}
}
print "</Select></td></Tr></table></body></html>";
I am learning Perl and have decided to create a web page that has
dropdowns populated by a postgreSQL DB. I've been able to do this and
the page looks fine but the code is awful. A chunk of the code is
posted below; can anyone suggest a way to clean this up and make it
more efficient? Namely, I am trying to consolidate the actual
population of the <SELECT> into one loop, and figure out a way to get
rid of the ugly "$i < scalar(@array)" line.
This all works, but I'm still learning Perl and haven't figured out
how to make it work good!
Thanks,
Tom
CODE BELOW:
===========
#!/opt/perl/bin/perl -wT
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
my $dbh = DBI->connect( "dbig:dbname=web" ) || die;
my @a = ('groupdescription','groups');
my @b = ('role','servers_role');
my $sth1 = $dbh->prepare("Select $a[0] from $a[1] order by $a[0]");
my $sth2 = $dbh->prepare("Select $b[0] from $b[1] order by $b[0]");
$sth1->execute;
$sth2->execute;
print $q->header,
$q->start_html,
$q->startform,
"<table><Tr><td>$a[0]</td>",
"<td><Select name=\"$a[0]\">";
while( @row1 = $sth1->fetchrow ) {
for( $i = 0; $i < scalar(@row1); $i++ ) {
print "<option value=\"$row1[$i]\">$row1[$i]";
}
}
print "</Select></td></Tr>",
"<Tr><td>$b[0]</td>",
"<td><Select name=\"$b[0]\">";
while( @row2 = $sth2->fetchrow ) {
for( $i = 0; $i < scalar(@row2); $i++ ) {
print "<option value=\"$row2[$i]\">$row2[$i]";
}
}
print "</Select></td></Tr></table></body></html>";