> On Mon, Jun 24, 2002 at 08:08:50AM -0700, Roy Rubin wrote:
> >Can someone provide an example of nested loops. I have it
> working, but I am
> >using *bad* code because I have had problems with referencing.
>
> Code roughly like this:
>
> my @l1;
> foreach my $outer (@outer_loop_list) {
> my %r1=(outer => $outer);
> # add more outer-loop stuff here in %r1
> my @l2;
> foreach my $inner (@inner_loop_list) {
> my %r2=(inner => $inner);
> # add more inner-loop stuff here in %r2
> push @l2,\%r2;
> }
> $r1{innerloop}=\@l2;
> push @l1,\%r1;
> }
> $tmpl->param(outerloop => \@l1);
>
> For a template like this:
>
> <table>
> <tmpl_loop name=outerloop>
> <tr><td><tmpl_var name=outer escape=html></td>
> <tmpl_loop name=innerloop>
> <td><tmpl_var name=inner escape=html></td>
> </tmpl_loop>
> </tr>
> </tmpl_loop>
> </table>
>
>
> Once you get the hang of it, you'll find that you can often omit some of
> the intermediate variables; this is the fully-laid-out version so that
> you can see what's going on.
>
> (Note that I haven't checked this particular code fragment, but it
> usually works first time when I do this for real...)
>
> Roger
Thanks Roger. How would I use the same concept and apply it to fetching
records from a database, something along the lines below. I am looking to
use the refernces that are already part of the DBI, instead of hard coding
the variables (as in $OfficeID, etc.) I hope this makes sense.
Thanks again.
===
$sql = qq{SELECT OfficeID, OfficeLocation FROM Office};
$sth = $dbh->prepare($sql) or bail_out($self);
$sth->execute() or bail_out($self);
#outer loop
while (($OfficeID, $OfficeLocation) = $sth->fetchrow_array()) {
$sql = qq{Select ID from Employees WHERE Office=$OfficeID};
$sth1 = $dbh->prepare($sql) or bail_out($self);
$sth1->execute() or bail_out($self);
#inner loop
while (($ID) = $sth1->fetchrow_array()) {
push @{whatever}, { ID => $ID};
}
push @OFFICES, {OfficeLocation => $OfficeLocation, EmpNames =>
\@{whatever}};
}
$thtml->param(OFFICES => \@OFFICES);
====
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]