For starters, your image_loop loop will not produce any output using a
nested loop structure. It's not supposed to.
I suspect your in_out_loop should be the following:
<TMPL_LOOP NAME=CATEGORY_ROWS>
OUTER CATEGORY : <TMPL_VAR NAME=name><br>
<TMPL_LOOP NAME=IMAGE_ROWS>
INNER IMAGE_NAME : <TMPL_VAR NAME=image_name><br>
<!-- Note the change of 'name' to 'category' -->
INSIDE OUT CATEGORY: <TMPL_VAR NAME=category><br>
</TMPL_LOOP>
</TMPL_LOOP>
Finally, I suspect that you are only receiving Halloween and not Halloween
and Xmas because you reuse the $sth variable in the inner loop. You're
blowing away the initial results. You need a new variable. I don't work
with mySQL (only Sybase which has a fantastic nsql() method), so I'm not
positive. I probably should have noticed that earlier. Try:
#2nd query
$sth2 = $dbh->prepare("select image_name, category from card_images
where category = '$name' order by image_name");
$sth2->execute;
$sth2->bind_columns(\my($image_name, $category));
# VERY Important step
my @image_rows = ();
#loop through results of 2nd query.
while($data = $sth2->fetchrow_hashref){
push @image_rows, { image_name => $image_name, category =>
$category };
}
push @category_rows, { name => $name, IMAGE_ROWS => \@image_rows
};
}
$sth2->finish; # Close both
$sth->finish;
...
Again, I'm not positive that that's the problem. But it's a good place to
start.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, November 26, 2002 4:26 PM
To: [EMAIL PROTECTED]
Subject: Re: [htmltmpl] looping through a DB result_set - LONG
Hi Dan,
On Tuesday 26 November 2002 11:50, Costantino, Dan Dan.Costantino-x-gs.com
|html::template| scribed:
> It appears that you're not creating your nested loop properly. For
> each iteration of the CATEGORY_ROWS array (ie for each hash), there
> needs to be a reference to an array of hashes, which is the IMAGE_ROWS
> inner loop.
Yes..I do apologize again for such a poor example of code to work with.. I
had tried nesting a few times and kept getting $hash errors... its been a
struggle and I fscked up'd a lot of code along the way. :-)
> push @category_rows, { name => $name, IMAGE_ROWS => \@image_rows };
> $template->param( { CATEGORY_ROWS => \@category_rows } );
> To accomplish this you will have to execute your 2nd query for each
> result of the first query:
thats the way I understood it myself..but alas, was not able to get it.
> my @category_rows = ();
> #loop through results of 1st query
>
<snippage of code>
>
> Please validate syntax as I haven't had an opportunity to check it
> myself.
It seems that the first query/loop is only grabbing the *first* of two
records(Halloween, Xmas) in the categories table, so something is still
amiss..looking at it.. to me it should work as you suggested.
Hopefully once it is getting all the "categories" from the table I can get
it worked out.
Any ideas from the code snippet below?
Once again thanks for your time.
Here is what I have for my nested loops.. and template
-------------------------------------------------------------
$sth = $dbh->prepare('select name from categories order by name');
$sth->execute;
$sth->bind_columns(\my($name));
my @category_rows = ();
#loop through results of 1st query
while($data = $sth->fetchrow_hashref){
#2nd query
$sth = $dbh->prepare("select image_name, category from card_images where
category = '$name' order by image_name");
$sth->execute;
$sth->bind_columns(\my($image_name, $category));
# VERY Important step
my @image_rows = ();
#loop through results of 2nd query.
while($data = $sth->fetchrow_hashref){
push @image_rows, { image_name => $image_name, category =>
$category };
}
push @category_rows, { name => $name, IMAGE_ROWS => \@image_rows };
}
$sth->finish;
$dbh->disconnect;
# open the html template
my $template = HTML::Template->new(filename => 'in_out_loop.html',
die_on_bad_params => 0,
global_vars => 1,
associate => $query);
# fill in the parameters
$template->param( { CATEGORY_ROWS => \@category_rows } );
---------------- Template Output ---------------------------
category_loop => Halloween
------------------
image_loop
------------------
in_out_loop
OUTER CATEGORY : Halloween
INNER IMAGE_NAME : pumpkin.jpg
INSIDE OUT CATEGORY: Halloween
------------------
Halloween
pumpkin.jpg
---------------- Template Output ---------------------------
---------------- Template ---------------------------
category_loop
<TMPL_LOOP NAME=CATEGORY_ROWS>
<TMPL_VAR NAME=name><br>
</TMPL_LOOP>
image_loop
<TMPL_LOOP NAME=IMAGE_ROWS>
<TMPL_VAR NAME=image_name> - <TMPL_VAR NAME=category><br>
</TMPL_LOOP>
in_out_loop
<TMPL_LOOP NAME=CATEGORY_ROWS>
OUTER CATEGORY : <TMPL_VAR NAME=name><br>
<TMPL_LOOP NAME=IMAGE_ROWS>
INNER IMAGE_NAME : <TMPL_VAR NAME=image_name><br>
INSIDE OUT CATEGORY: <TMPL_VAR NAME=name><br>
</TMPL_LOOP>
</TMPL_LOOP>
<TMPL_LOOP NAME="CATEGORY_ROWS">
<TMPL_VAR NAME="name"><br>
<TMPL_LOOP NAME="IMAGE_ROWS">
<TMPL_VAR NAME="image_name"> <br>
</TMPL_LOOP>
</TMPL_LOOP>
---------------- Template ---------------------------
- thanks -
--
michael
-------------------------------------------------------
This SF.net email is sponsored by: Get the new Palm Tungsten T
handheld. Power & Color in a compact size!
http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en
_______________________________________________
Html-template-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/html-template-users
-------------------------------------------------------
This SF.net email is sponsored by: Get the new Palm Tungsten T
handheld. Power & Color in a compact size!
http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en
_______________________________________________
Html-template-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/html-template-users