multiple fields from SQL query
Mark Haney <markh-rOuQeLG7NUhWk0Htik3J/[email protected]> Thu, 24 May 2012 10:52:19 -0400
| Newsgroups | gmane.comp.lang.perl.modules.template-toolkit |
|---|---|
| Message-ID | <[email protected]> |
I've got a query in my perl code to pull 2 fields from a database
(abbreviation, factory_id), one of which is used to populate a dropdown
box (abbreviation). However, I will need to pass /both/ fields to the
CGI script that I'm calling from the form that has the dropdown box.
How do I do that? Here's what I have so far:
shift_rpt.pl:
my $sql = qq/SELECT factory_id, abbreviation from factory/;
my $sth = $dbh->prepare($sql);
$sth->execute();
my $row_factory;
my @rows_factory;
while ($row_factory = $sth->fetchrow_array)
{
push @rows_factory, $row_factory; # This moves each row into the
array @rows
}
$sth->finish();
The processing part of the perl code is below:
my $file = 'templates/shift_rpt.html';
my $vars = {
'cgi' =>CGI->new(),
'factory' => \@rows_factory, # This moves the data from the array into
a var that gets carried into the template
'shifts' => \@rows_shift # This moves the custom shifts names into the
dropdown box for the reports
};
print "Content-type: text/html\n\n";
my $template = Template->new({
# where to find template files
INCLUDE_PATH => ['/home/markh/work/epic.git/code/embmon-web/src',
'/home/markh/work/epic.git/code/embmon-web/lib'],
# pre-process lib/config to define any extra values
PRE_PROCESS => ['config']
});
shift.html (the template):
<p style="text-align: left;"><strong>Select Factory:</strong><select
name="factory"><OPTION>
[% FOREACH name IN factory %]
<option value="" size="20">[% name %]</option>
[% END %]
</select>
--
Mark Haney
Software Developer/Consultant
AB Emblem
markh-rOuQeLG7NUhWk0Htik3J/[email protected]
Linux marius.homelinux 3.3.6-3.fc16.x86_64 GNU/Linux