Re: multiple fields from SQL query --Update

Theunis De Klerk <theunis-xn/[email protected]> Thu, 24 May 2012 20:53:39 +0200
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <[email protected]>
On 5/24/2012 8:14 PM, Mark Haney wrote:
> On 05/24/2012 10:52 AM, Mark Haney wrote:
>>
>> 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 just can't seem to get the logic of this straight in my head.  Maybe 
> I'm over-complicating things because of the toolkit.  Seems to me like 
> the toolkit is sitting there complicating my thought process when it 
> really shouldn't.

Hi Mark,

I am not sure I understand exactly what you are trying to achieve, but I 
think you are right about putting it into a scalar.

As an example, how does this work for you :

my @rows_factory;
while ( my @factory_data = $sth->fetchrow_array() )
{
     push @rows_factory, {'id' => $factory_data[0], 'abbr' => 
$factory_data[1]};
}

I normally do that when I want to do this in the template :

<select name="factory">
       [% FOREACH factory IN factories %]
<option value="[% factory.id %]" size="20">[% factory.abbr %]</option>
       [% END %]
</select>

Regards,
Theunis