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 confused 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 => [EMAIL PROTECTED], -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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>