Re: populating dropdown box from db

<eric.berg-W7cInXPOPQlWk0Htik3J/[email protected]> Wed, 9 May 2012 12:22:23 -0400
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <B0011508DF5C184ABEC7A8978D7AB5E80C389B5EBE@NYKPCMMGMB03.INTRANET.BARCAPINT.COM>
Use the TT wrapper for the CGI module and simply pass in an array of values and a hash of value => label pairs as per the docs. Check the docs for CGI as well as the TT CGI plugin.

[% USE CGI %]
[% CGI.popup_menu({ Name => 'client_type',
                    Values => client_types,
                    Labels => client_type_labels,
                 }) %]



> -----Original Message-----
> From: templates-bounces-MnLy+PD7ppGDO3lXZvfOKmD2FQJk+8+b@public.gmane.org [mailto:templates-
> [email protected]] On Behalf Of Octavian Rasnita
> Sent: Wednesday, May 09, 2012 11:39 AM
> To: Mark Haney; [email protected]
> Subject: Re: [Templates] populating dropdown box from db
> 
> 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
> 
> 
> _______________________________________________
> templates mailing list
> [email protected]
> http://mail.template-toolkit.org/mailman/listinfo/templates
_______________________________________________

This e-mail may contain information that is confidential, privileged or otherwise protected from 
disclosure. If you are not an intended recipient of this e-mail, do not duplicate or redistribute 
it by any means. Please delete it and any attachments and notify the sender that you have received 
it in error. Unless specifically indicated, this e-mail is not an offer to buy or sell or a 
solicitation to buy or sell any securities, investment products or other financial product or 
service, an official confirmation of any transaction, or an official statement of Barclays. Any 
views or opinions presented are solely those of the author and do not necessarily represent those 
of Barclays. This e-mail is subject to terms available at the following link: www.barcap.com/emaildisclaimer. 
By messaging with Barclays you consent to the foregoing.  Barclays offers premier investment banking 
products and services to its clients through Barclays Bank PLC, a company registered in England 
(number 1026167) with its registered office at 1 Churchill Place, London, E14 5HP.  This email may 
relate to or be sent from other members of the Barclays Group.

_______________________________________________