Ken:

You need to refer to the inner loop as an array reference within the data hierarchy of the first loop.  Currently you have them both on the same level.  Try this code:

#!/usr/bin/perl
use DBI;
use CGI;
use HTML::Template;

my $dbsource = "DBI:mysql:dbname=msearch;host=localhost";
my $dbuser   = "chrisp";
my $dbpasswd = "cockfuck";

my $template = HTML::Template->new(filename  => 'index.tmpl');
my $bychar = 'c';
my @URLS = ();
my @ROWS = ();

my $dbh = DBI->connect($dbsource, $dbuser, $dbpasswd);

## Query 1
my $sth = $dbh=>prepare("SELECT model_url FROM models where model_name = ?");
$sth->execute('Christine');
while(my $data = "" {
    push (@URLS, { MODEL_URL => $data->{model_url} }); ## Store info from first query
}
$sth->finish();

## Query 2
$sth = $dbh->prepare("SELECT DISTINCT model_name as model_name, model_thumb FROM models WHERE model_firstchar = ? GROUP BY model_name");
$sth->execute($bychar) || die &error($DBI::errstr);
while (my $data = "" {
    push (@ROWS, { MODEL_NAME  => $data->{model_name},
                   MODEL_THUMB => $data->{model_thumb},
                   URLLOOP     => [EMAIL PROTECTED],
               });
}
$sth->finish();

$template->param( ROWS => [EMAIL PROTECTED] );
print $template->output;


--Jayson





"Chris" <[EMAIL PROTECTED]>
Sent by: [EMAIL PROTECTED]

11/17/03 03:40 PM

       
        To:        "Kenneth Gonsalves" <[EMAIL PROTECTED]>, <[EMAIL PROTECTED]>
        cc:        
        Subject:        RE: [htmltmpl] MySQL -> HTML::Template - WITH source



Hey,

Here is a "working" example of what im trying to do:

Perl Code:

#!/usr/bin/perl
use DBI;
use CGI;
use HTML::Template;

my $dbsource = "DBI:mysql:dbname=msearch;host=localhost";
my $dbuser = "chrisp";
my $dbpasswd = "cockfuck";

my $template = HTML::Template->new(filename  => 'index.tmpl');

my $bychar = "c";

my $dbh = DBI->connect($dbsource, $dbuser, $dbpasswd);
my $query = "SELECT DISTINCT model_name as model_name, model_thumb FROM
models WHERE model_firstchar='$bychar' GROUP BY model_name";
my $sth = $dbh->prepare($query);
$sth->execute() || die &error($DBI::errstr);

my $dbh2 = DBI->connect($dbsource, $dbuser, $dbpasswd);
my $query2 = "SELECT model_url FROM models where model_name =
'Christine'";
my $sth2 = $dbh2->prepare($query2);

while (my $data = ""> {
       push (@loopdata, $data);

       $sth2->execute() || die &error($DBI::errstr);

       while (my $data2 = $sth2->fetchrow_hashref())
       {
               push(@loopdata2, $data2);
       }
}


$template->param('URLLOOP' => [EMAIL PROTECTED], 'ROWS' => [EMAIL PROTECTED]);

print $template->output;


My HTML Template - WORKING BUT UNWANTED RESULTS:
<TMPL_LOOP ROWS>
<TMPL_VAR NAME=MODEL_NAME>
<TMPL_VAR NAME=MODEL_THUMB>
Can be seen at:
</TMPL_LOOP>
<TMPL_LOOP URLLOOP>
<TMPL_VAR NAME=MODEL_URL>
</TMPL_LOOP>

My HTML Template - THAT I WANT TO WORK:
<TMPL_LOOP ROWS>
<TMPL_VAR NAME=MODEL_NAME>
<TMPL_VAR NAME=MODEL_THUMB>
Can be seen at:
<TMPL_LOOP URLLOOP>
<TMPL_VAR NAME=MODEL_URL>
</TMPL_LOOP>
</TMPL_LOOP>
<br>


It seems once i put the loop within the loop, I get the following error:

HTML::Template : Attempt to set nonexistent parameter 'urlloop' - this
parameter name doesn't match any declarations in the template file :
(die_on_bad_params => 1) at test.pl line 36

Im really not sure what to do here

Thanks!


> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Behalf Of
> Chris
> Sent: Monday, November 17, 2003 1:28 PM
> To: Kenneth Gonsalves; [EMAIL PROTECTED]
> Subject: RE: [htmltmpl] MySQL -> HTML::Template
>
>
> Thanks for the link, it helped me very much!
>
> Now once again im stuck...
>
> How do I do nested loops??
>
> Using the perlmonks example:
>
> <TMPL_LOOP ROWS>
>     <tr bgcolor="#<TMPL_IF ODD>666666<TMPL_ELSE>EEEEEE</TMPL_IF>">
>         <td><TMPL_VAR NAME=ID></td>
>         <td><TMPL_VAR NAME=NAME></td>
>         <td><TMPL_VAR NAME=PRICE></td>
>     </tr>
> </TMPL_LOOP>
>
> If I wanted to do say:
>
> <TMPL_LOOP ROWS>
>     <tr bgcolor="#<TMPL_IF ODD>666666<TMPL_ELSE>EEEEEE</TMPL_IF>">
>         <td><TMPL_VAR NAME=ID></td>
>                                                    <TMPL_LOOP INNERROWS>
>                                                                     <TMPL_VAR NAME=SUBID>
>                                                    </TMPL_LOOP>
>         <td><TMPL_VAR NAME=NAME></td>
>         <td><TMPL_VAR NAME=PRICE></td>
>     </tr>
> </TMPL_LOOP>
>
>
> For the life of me, I can't figure out how to do it...
>
> I have repeatedly destroyed my own code, so I will use the perlmonks
> code..
>
> This is what I have been trying, is it even *close* to right???
>
> while ($sth->fetch) {
>     my $real_price = $price < $saleprice ? $price : $saleprice;
>     $name =~ s/\b(\w)(\w+)\b/\U$1\L$2/g;
>     push @rows, { ID => lc($id), PRICE => $real_price, NAME => $name,
> +ODD => $i++ %2 };
>     $sum += $real_price;
>                                   while ($sth2->fetch) {
>                                                    push @innerrows, { SUBID => $sub_id };
>                                   }
> }
>
> $sth->finish;
> $sth2->finish;
>
> my $t = HTML::Template->new(filename => 'db2.tmpl', @tmpl_opts);
> $t->param(ROWS => [EMAIL PROTECTED], INNERROWS => [EMAIL PROTECTED]);
>
> I know this isn't my code, but like I said, iv destroyed mine over and
> over.. But this gives you the idea of what iv been trying, each time
with
> failure.
>
> Any help is very much appreciated as I am very frustrated :)
>
> Thx!
> -c
>
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] Behalf Of
> > Kenneth Gonsalves
> > Sent: Sunday, November 16, 2003 2:38 AM
> > To: Chris; [EMAIL PROTECTED]
> > Subject: Re: [htmltmpl] MySQL -> HTML::Template
> >
> >
> > On Sunday 16 November 2003 12:17, Chris wrote:
> > > Hey all,
> > >
> > >                  Im new to HTML:Template, and it looks pretty nice, but I
> > have not seen
> > > anything related to using MySQL output. Basically all I really
> > want to do
> > > is be able to give my web designers tags like: %%SEARCH_RESULTS%%
> which
> > > when the page is parsed by my cgi using HTML:Template will
> > recieve my sql
> > > results. Is anybody doing this?

> > >
> > > Any pointers very much appreciated:)
> > > -c
> > there is a very nice tutorial on this in perlmonks.org
> > --
> > regards
> > kg
> >
> > http://www.ootygolfclub.org
> >
> >
> > -------------------------------------------------------
> > This SF. Net email is sponsored by: GoToMyPC
> > GoToMyPC is the fast, easy and secure way to access your computer from
> > any Web browser or wireless device. Click here to Try it Free!
> >
https://www.gotomypc.com/tr/OSDN/AW/Q4_2003/t/g22lp?Target=mm/g22lp.tmpl
> > _______________________________________________
> > Html-template-users mailing list
> > [EMAIL PROTECTED]
> > https://lists.sourceforge.net/lists/listinfo/html-template-users
>





Jefferies archives and reviews outgoing and incoming e-mail. It may be produced at the request of regulators or in connection with civil litigation.
Jefferies accepts no liability for any errors or omissions arising as a result of transmission. Use by other than intended recipients is prohibited.

Reply via email to