Re: populating dropdown box from db

"Octavian Rasnita" <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <EEA5824CC9944F139AE57461AB212279@octavianf303f0>
From: "Mark Haney" <markh-rOuQeLG7NUhWk0Htik3J/[email protected]>
Subject: [Templates] populating dropdown box from db


> HI all, I'm rather new to the TT, but so far I'm liking it's flexibility. 
> The badger book is a great resource as well.  But, I'm having an issue I 
> can't seem to find an answer to and am hoping the list can help me.
>
> I want to populate a drop down box with data from a database.  I'm pulling 
> the factory name from a table to put in the drop down box so that the user 
> can select which factory to pull shift reports from. Here's what I have so 
> far:
>
> in the .pl file:
>
>>  my $cgi = new CGI;
>>     my $dbh = DBI->connect(config('data_source'), config('username'), 
>> config('password'));
>>     my $dao = EmbMon::DAO->new($dbh);
>>     my $sql = qq/SELECT abbreviation from factory/;
>>     my $sth = $dbh->prepare($sql);
>>     $sth->execute();
>>
>>     while ($row = $sth->fetchrow_array)
>>     {
>>     push @rows, $row;
>>     }
>>
>>     my $file = 'shift_rpt.html';
>>     my $vars = {
>> 'cgi' =>CGI->new(),
>> 'version' => "0.5.8.2012",
>> 'factory' => \@rows
>>     };
>
>>     $template->process($file, $vars)
>>         || die "Template process failed: ", $template->error(), "\n";
>
>
> in the HTML template:
>
>> <form name="myform" method="POST" action="/cgi-bin/shift_rpt.cgi">
>
>> [% FOREACH factory IN factory %]
>>     <p style="text-align: left;"><strong>Select 
>> Factory:</strong><br><select name="factory"><OPTION>
>>     <option value=some_value>$factory</option>
>>     </select>
>>     <input class="button" type="submit" value="Run Report">
>>
>> </form>
>
>
> Is this the proper way to do this, or is there a better/more flexible way? 
> Am I missing something?




Hi,

I think you want something like:


<p style="text-align: left;"><strong>Select Factory:</strong><br><select 
name="factory">
[% FOREACH some_value IN factory %]
<option value="[% some_value %]">[% some_value %]</option>
[% END %]
</select>

Octavian
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.