For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > November 2004 > question about doing it right in CGI









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author question about doing it right in CGI
Taylor Lewick

2004-11-19, 8:55 pm

Hi all, I have been using perl for sometime for CGI scripts, but have
always used the print content-type:html version of doing things.

I would like to learn a better way with the CGI module, but when I read
the docs I find it pretty easy to get as to whether I should
use the object oriented method, or the functional method.

Also, because my script is a cgi form that gets some of the select
fields from a mysql database, I am not sure how to convert that over. I
don't know how I would make the select field work the same.

I am not asking for someone to rewrite my project, merely provide me
with some examples of how they would write the same code using the cgi
module so I can figure this out a bit better...

On my form, I am querying a database for a list of names and loading
them into a select box. But I found out people want to enter more than
one name at a time, so I loop through 15 times, and they can select up
to 15 names... They select a name, but I store the name's id, so it
acts as a lookup field...

Here is how I do this now..
#Connect to database
print "<table>\n";
for (1..15) {
print "<td nowrap>\n";
$query_teams=3D("select id, name from teams");
$sth =3D $dbh->prepare($query_teams);
$sth->execute();
$sth->bind_columns(\$id, \$name);
print "<select name=3D'away_team$_'>"; #$_ traps which pass of the loop
we are in i.e., 3rd pass, 4th pass, etc
print "<option value=3D'0'></option>\n";
while($sth->fetch()) {
print "<option value=3D'$id'>$name</option>\n";
}
print "</select>\n";
$sth->finish();
print "</td>\n";
} #end for loop
print "</table>\n";
#disconnect from database

How would I start to convert this with the CGI module. My problems thus
far are on a popup menu, how do I specify the field variable that I grab
is the ID, while the displayed value is another, and how can I say the
first value should be 0, in case they do not enter anything?

Thanks in advance,
Taylor

Bob Showalter

2004-11-19, 8:55 pm

Lewick, Taylor wrote:
> Hi all, I have been using perl for sometime for CGI scripts, but have
> always used the print content-type:html version of doing things.
>
> I would like to learn a better way with the CGI module, but when I
> read the docs I find it pretty easy to get as to whether I
> should use the object oriented method, or the functional method.


Either is fine IMO. The function method requires you to import the correct
symbols, which can be a bit tricky.

>
> Also, because my script is a cgi form that gets some of the select
> fields from a mysql database, I am not sure how to convert that over.
> I don't know how I would make the select field work the same.
>
> I am not asking for someone to rewrite my project, merely provide me
> with some examples of how they would write the same code using the cgi
> module so I can figure this out a bit better...
>
> On my form, I am querying a database for a list of names and loading
> them into a select box. But I found out people want to enter more
> than one name at a time, so I loop through 15 times, and they can
> select up to 15 names... They select a name, but I store the name's
> id, so it acts as a lookup field...
>
> Here is how I do this now..
> #Connect to database
> print "<table>\n";
> for (1..15) {
> print "<td nowrap>\n";
> $query_teams=("select id, name from teams");
> $sth = $dbh->prepare($query_teams);
> $sth->execute();
> $sth->bind_columns(\$id, \$name);
> print "<select name='away_team$_'>"; #$_ traps which pass of the loop
> we are in i.e., 3rd pass, 4th pass, etc
> print "<option value='0'></option>\n";
> while($sth->fetch()) {
> print "<option value='$id'>$name</option>\n";
> }
> print "</select>\n";
> $sth->finish();
> print "</td>\n";
> } #end for loop
> print "</table>\n";
> #disconnect from database
>
> How would I start to convert this with the CGI module. My problems
> thus far are on a popup menu, how do I specify the field variable
> that I grab is the ID, while the displayed value is another, and how
> can I say the first value should be 0, in case they do not enter
> anything?


You're querying the database 15 times; I definitely wouldn't do that.

I ususally grab all the rows into an arrayref using something like this:

my $rows = $dbh->selectall_arrayref('select id, name from teams');

This handles all the DBI calls one one swoop.

For the CGI module's popup_menu, you need two things:

1) a list of values for the <option> elements, and
2) a hash of value => description pairs for the labels

Here's how to get the labels hash from the rows fetched above:

my %labels = ( 0 => '', map @$_, @$rows );

You can extract the value list from the hash keys:

my @values = sort { $a <=> $b } keys %labels;

Now you can generate the table by using CGI's routines like this (OO style):

print $q->start_table,
$q->start_Tr,
$q->td({ -nowrap => 'nowrap' }, [ map $q->popup_menu(
-name => "away_team$_",
-labels => \%labels,
-values => \@values,
-default => '0',
), 1 .. 15 ]),
$q->end_Tr,
$q->end_table;

CGI let's you use start_xxx and end_xxx methods to generate just a start or
end tag.

If you pass an arrayref to a method like td(), CGI will generate multiple
elements, one for each entry in the array. This lets us generate all 15 <td>
elements in one call. Look in the CGI docs under "THE DISTRIBUTIVE PROPERTY
OF HTML SHORTCUTS"

The map() function generates a list of <select> objects.

HTH
Sean Davis

2004-11-22, 3:55 pm

I use the object-oriented interface pretty much exclusively. There are good
examples of creating a popup menu in the documentation for CGI. (You know
about http://search.cpan.org?) Also, there are multiple websites describing
use of CGI.pm. I would suggest that you start with a simple cgi script
using CGI.pm that does nothing other than create a popup menu. Use your web
browser's view source command to see what is generated and then try to
modify your CGI script to make the HTML appear as you like. Other modules
that you might find interesting are Class::DBI and html::fillinform

Sean

----- Original Message -----
From: "Lewick, Taylor" <tlewick@hrblock.com>
To: <beginners-cgi@perl.org>
Sent: Friday, November 19, 2004 2:51 PM
Subject: question about doing it right in CGI


Hi all, I have been using perl for sometime for CGI scripts, but have
always used the print content-type:html version of doing things.

I would like to learn a better way with the CGI module, but when I read
the docs I find it pretty easy to get as to whether I should
use the object oriented method, or the functional method.

Also, because my script is a cgi form that gets some of the select
fields from a mysql database, I am not sure how to convert that over. I
don't know how I would make the select field work the same.

I am not asking for someone to rewrite my project, merely provide me
with some examples of how they would write the same code using the cgi
module so I can figure this out a bit better...

On my form, I am querying a database for a list of names and loading
them into a select box. But I found out people want to enter more than
one name at a time, so I loop through 15 times, and they can select up
to 15 names... They select a name, but I store the name's id, so it
acts as a lookup field...

Here is how I do this now..
#Connect to database
print "<table>\n";
for (1..15) {
print "<td nowrap>\n";
$query_teams=("select id, name from teams");
$sth = $dbh->prepare($query_teams);
$sth->execute();
$sth->bind_columns(\$id, \$name);
print "<select name='away_team$_'>"; #$_ traps which pass of the loop
we are in i.e., 3rd pass, 4th pass, etc
print "<option value='0'></option>\n";
while($sth->fetch()) {
print "<option value='$id'>$name</option>\n";
}
print "</select>\n";
$sth->finish();
print "</td>\n";
} #end for loop
print "</table>\n";
#disconnect from database

How would I start to convert this with the CGI module. My problems thus
far are on a popup menu, how do I specify the field variable that I grab
is the ID, while the displayed value is another, and how can I say the
first value should be 0, in case they do not enter anything?

Thanks in advance,
Taylor


--
To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
For additional commands, e-mail: beginners-cgi-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com