On Thu, 24 May 2012 14:14:48 -0400
Mark Haney <ma...@abemblem.com> wrote:

> On 05/24/2012 10:52 AM, Mark Haney wrote:
> > 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();
> >
> 
> I've had a couple of good suggestions on how to handle this, but I'm 
> obviously missing something.  In the WHILE loop above, I'm not
> getting both fields from the query.  I.e. I'm getting the last
> element of the query (abbrev) pushed into the array @rows_factory but
> not the factory_id.  I'm guessing it's due to the fact that I'm
> fetching the row into a scalar ($row_factory) and not an array.
> Correct?

I'd recommend using fetchrow_hashref instead of fetchrow_array.  It
returns a reference to a hash for each row, and allows you to
access the columns by name.

Then your for loop would work like this:

          [% FOREACH row IN factory %]
          <option value="[% row.factory_id %]" size="20">[%
          row.abbreviation %]</option> 
          [% END %]


-- 

C. Chad Wallace, B.Sc.
The Lodging Company
http://www.lodgingcompany.com/
OpenPGP Public Key ID: 0x262208A0


_______________________________________________
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to