grath has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use CGI qw/:standard :html3/; use CGI::Carp qw(fatalsToBrowser); use DBI; use DBD::mysql; my $DBName = "test"; my $DBHost = "localhost"; my $DBUser = "test"; my $DBPass = "dev"; my $DBType = "mysql"; my $DBPort = "3306"; sub db_connect { my ($result); $result = DBI->connect( "DBI:$DBType:database=$DBName;host=$DBHost +", "$DBUser", "$DBPass", { 'RaiseError' => 0 } ) || &error("Unable to + connect to database"); return $result; } sub db_query { my ($result, $query); $query = $_[0]; $result = &db_connect->prepare($query) || &error("Unable to query +the database"); $result->execute(); return $result; } # outer loop my $query1 = &db_query("SELECT id, title FROM sections"); while (my $rows1 = $query1->fetchrow_hashref()) { print "Section: " . $rows1->{title}."\n"; my $sectionid = $rows1->{id}; # inner loop my $query2 = &db_query("SELECT title FROM pages WHERE sectioni +d = $sectionid"); while (my $rows2 = $query2->fetchrow_hashref()) { print "-> Page: " . $rows2->{title}."\n"; } }
<TMPL_LOOP NAME="SECTIONS"> Section: <TMPL_VAR NAME="TITLE"><br> <TMPL_LOOP NAME="PAGES"> -> Page: <TMPL_VAR NAME="TITLE"><br> </TMPL_LOOP> </TMPL_LOOP>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTML::Template: Howto make a inner/outer loop?
by jeffa (Bishop) on Apr 05, 2004 at 20:25 UTC | |
|
Re: HTML::Template: Howto make a inner/outer loop?
by gryphon (Abbot) on Apr 05, 2004 at 20:18 UTC | |
|
Re: HTML::Template: Howto make a inner/outer loop?
by matija (Priest) on Apr 05, 2004 at 20:32 UTC | |
|
Re: HTML::Template: Howto make a inner/outer loop?
by dragonchild (Archbishop) on Apr 05, 2004 at 19:42 UTC | |
|
Re: HTML::Template: Howto make a inner/outer loop?
by Elijah (Hermit) on Apr 05, 2004 at 19:30 UTC |