Being able to see this raw data structure helped me to figure out how to create it programatically for HTML::Template.%loop_data = ( rows => [ { cells => [ { data => 'one'}, { data => 'ein'}, ], }, { cells => [ { data => 'two'}, { data => 'zwei'}, ], }, { cells => [ { data => 'three'}, { data => 'drei'}, ], }, ], );
The original data I posted was not in the format I was actually working with, but I presented it as such to enable me to figure out exactly what HTML::Template wanted, and how to create that myself (rather that using another-monks map-statement-on-a-plate that I couldn't understand).#!/usr/bin/perl -wT use strict; use CGI; use DBI; use HTML::Template; use vars qw/ $q $dbh $sth $rv $rc $template $html @columns @headings_data @rows_data $sql / +; @columns = qw/ prod_code dep_1 dep_2 dep_3 /; @headings_data = map {{ cell_data => $_ }} @columns; $sql = 'SELECT ' . join(', ', @columns) . ' FROM products WHERE dep_1= +01'; $dbh = DBI->connect("dbi:mysql:database:host", "user", "pass") or print_error("Could not connect to Database", $DBI::errstr); $sth = $dbh->prepare( $sql ) or die("Couldn't prepare the database", "$DBI::errstr"); $rv = $sth->execute(@_) or die("Couldn't execute database", "$DBI::errstr"); while ( my @data = $sth->fetchrow_array ) { my @cells = map {{ cell_data => $_ }} @data; push @rows_data, { cells => \@cells }; } $html = do { local $/; <DATA> }; $template = HTML::Template->new(scalarref => \$html); $template->param( headings => \@headings_data, rows => \@rows_data ); $q = new CGI; print $q->header; print $template->output; $dbh->disconnect; exit; __DATA__ <html> <body> <table> <tr> <!-- TMPL_LOOP NAME=headings --> <td><!-- TMPL_VAR NAME=cell_data --></td> <!-- /TMPL_LOOP --> </tr> <!-- TMPL_LOOP NAME=rows --> <tr> <!-- TMPL_LOOP NAME=cells --> <td><!-- TMPL_VAR NAME=cell_data --></td> <!-- /TMPL_LOOP --> </tr> <!-- /TMPL_LOOP --> </table> </body> </html>
In reply to Re: (fireartist)HTML table with HTML::Template and loop in a loop
by fireartist
in thread HTML table with HTML::Template and loop in a loop
by fireartist
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |