RE: [htmltmpl] MySQL -> HTML::Template - WITH source
Brought to you by:
samtregar
From: Jayson P. <JP...@je...> - 2003-11-17 21:13:11
|
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 =3D "DBI:mysql:dbname=3Dmsearch;host=3Dlocalhost"; my $dbuser =3D "chrisp"; my $dbpasswd =3D "cockfuck"; my $template =3D HTML::Template->new(filename =3D> 'index.tmpl'); my $bychar =3D 'c'; my @URLS =3D (); my @ROWS =3D (); my $dbh =3D DBI->connect($dbsource, $dbuser, $dbpasswd); ## Query 1 my $sth =3D $dbh=3D>prepare("SELECT model=5Furl FROM models where model=5Fna= me =3D =3F"); $sth->execute('Christine'); while(my $data =3D $sth->fetchrow=5Fhashref) { push (@URLS, { MODEL=5FURL =3D> $data->{model=5Furl} }); ## Store info f= rom first query } $sth->finish(); ## Query 2 $sth =3D $dbh->prepare("SELECT DISTINCT model=5Fname as model=5Fname, model=5Fthumb FROM models WHERE model=5Ffirstchar =3D =3F GROUP BY model=5Fn= ame"); $sth->execute($bychar) || die &error($DBI::errstr); while (my $data =3D $sth->fetchrow=5Fhashref) { push (@ROWS, { MODEL=5FNAME =3D> $data->{model=5Fname}, MODEL=5FTHUMB =3D> $data->{model=5Fthumb}, URLLOOP =3D> \@URLS, }); } $sth->finish(); $template->param( ROWS =3D> \@ROWS ); print $template->output; --Jayson "Chris" <chr...@co...> Sent by: htm...@li... 11/17/03 03:40 PM To: "Kenneth Gonsalves" <la...@th...>, <htm...@li...> 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 =3D "DBI:mysql:dbname=3Dmsearch;host=3Dlocalhost"; my $dbuser =3D "chrisp"; my $dbpasswd =3D "cockfuck"; my $template =3D HTML::Template->new(filename =3D> 'index.tmpl'); my $bychar =3D "c"; my $dbh =3D DBI->connect($dbsource, $dbuser, $dbpasswd); my $query =3D "SELECT DISTINCT model=5Fname as model=5Fname, model=5Fthumb F= ROM models WHERE model=5Ffirstchar=3D'$bychar' GROUP BY model=5Fname"; my $sth =3D $dbh->prepare($query); $sth->execute() || die &error($DBI::errstr); my $dbh2 =3D DBI->connect($dbsource, $dbuser, $dbpasswd); my $query2 =3D "SELECT model=5Furl FROM models where model=5Fname =3D 'Christine'"; my $sth2 =3D $dbh2->prepare($query2); while (my $data =3D $sth->fetchrow=5Fhashref()) { push (@loopdata, $data); $sth2->execute() || die &error($DBI::errstr); while (my $data2 =3D $sth2->fetchrow=5Fhashref()) { push(@loopdata2, $data2); } } $template->param('URLLOOP' =3D> \@loopdata2, 'ROWS' =3D> \@loopdata); print $template->output; My HTML Template - WORKING BUT UNWANTED RESULTS: <TMPL=5FLOOP ROWS> <TMPL=5FVAR NAME=3DMODEL=5FNAME> <TMPL=5FVAR NAME=3DMODEL=5FTHUMB> Can be seen at: </TMPL=5FLOOP> <TMPL=5FLOOP URLLOOP> <TMPL=5FVAR NAME=3DMODEL=5FURL> </TMPL=5FLOOP> My HTML Template - THAT I WANT TO WORK: <TMPL=5FLOOP ROWS> <TMPL=5FVAR NAME=3DMODEL=5FNAME> <TMPL=5FVAR NAME=3DMODEL=5FTHUMB> Can be seen at: <TMPL=5FLOOP URLLOOP> <TMPL=5FVAR NAME=3DMODEL=5FURL> </TMPL=5FLOOP> </TMPL=5FLOOP> <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=5Fon=5Fbad=5Fparams =3D> 1) at test.pl line 36 Im really not sure what to do here Thanks! > -----Original Message----- > From: htm...@li... > [mailto:htm...@li...]On Behalf Of > Chris > Sent: Monday, November 17, 2003 1:28 PM > To: Kenneth Gonsalves; htm...@li... > 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=3F=3F > > Using the perlmonks example: > > <TMPL=5FLOOP ROWS> > <tr bgcolor=3D"#<TMPL=5FIF ODD>666666<TMPL=5FELSE>EEEEEE</TMPL=5FIF>">= > <td><TMPL=5FVAR NAME=3DID></td> > <td><TMPL=5FVAR NAME=3DNAME></td> > <td><TMPL=5FVAR NAME=3DPRICE></td> > </tr> > </TMPL=5FLOOP> > > If I wanted to do say: > > <TMPL=5FLOOP ROWS> > <tr bgcolor=3D"#<TMPL=5FIF ODD>666666<TMPL=5FELSE>EEEEEE</TMPL=5FIF>">= > <td><TMPL=5FVAR NAME=3DID></td> > <TMPL=5FLOOP INNERROWS> > <TMPL=5FVAR= NAME=3DSUBID> > </TMPL=5FLOOP> > <td><TMPL=5FVAR NAME=3DNAME></td> > <td><TMPL=5FVAR NAME=3DPRICE></td> > </tr> > </TMPL=5FLOOP> > > > 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=3F=3F=3F > > while ($sth->fetch) { > my $real=5Fprice =3D $price < $saleprice =3F $price : $saleprice; > $name =3D~ s/\b(\w)(\w+)\b/\U$1\L$2/g; > push @rows, { ID =3D> lc($id), PRICE =3D> $real=5Fprice, NAME =3D> $na= me, > +ODD =3D> $i++ %2 }; > $sum +=3D $real=5Fprice; > while ($sth2->fetch) { > push @innerrows, { SUBID =3D> $sub=5Fid }; > } > } > > $sth->finish; > $sth2->finish; > > my $t =3D HTML::Template->new(filename =3D> 'db2.tmpl', @tmpl=5Fopts); > $t->param(ROWS =3D> \@rows, INNERROWS =3D> \@innerrows); > > 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: htm...@li... > > [mailto:htm...@li...]On Behalf Of > > Kenneth Gonsalves > > Sent: Sunday, November 16, 2003 2:38 AM > > To: Chris; htm...@li... > > 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=5FRESULTS%% > which > > > when the page is parsed by my cgi using HTML:Template will > > recieve my sql > > > results. Is anybody doing this=3F > > > > > > 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=5F2003/t/g22lp=3FTarget=3Dmm/g22lp.tm= pl > > =5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F= =5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F > > Html-template-users mailing list > > Htm...@li... > > https://lists.sourceforge.net/lists/listinfo/html-template-users > Jefferies archives and reviews outgoing and incoming e-mail. It may be prod= uced at the request of regulators or in connection with civil litigation. Jefferies accepts no liability for any errors or omissions arising as a resu= lt of transmission. Use by other than intended recipients is prohibited. |