use strict; use warnings; use DBI; use HTML::Entities; use HTML::Template; my $dbh = DBI->connect( qw(DBI:vendor:database:host user pass), {RaiseError=>1} ); # change these two vars to suite your needs: my @arg = (42); my $sql = 'select foo,bar from baz where qux = ?'; my $sth = $dbh->prepare($sql); $sth->execute(@arg); my $stmt = $sth->{Statement}; my $field = $sth->{NAME}; my $row = $sth->fetchall_arrayref(); my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param( query => $stmt, fields => [ map { { field => ucfirst lc $_ } } @$field ], rows => [ map {{ cols => [ map { { data => defined $_ ? encode_entities $_ : '&nbsp;' } } @$_ ] }} @$row ], ); print $tmpl->output; $dbh->disconnect; __DATA__ <html> <head> <title>Dynamic table template</title> <style type="text/css"> <!-- table { border-style: outset; border-width: thin; width: 70% } th { border-style: ridge; } td { border-style: inset; } --> </style> </head> <body> <table> <thead> <caption><tmpl_var query></caption> <tr> <tmpl_loop fields> <th><tmpl_var field></th> </tmpl_loop> </tr> </thead> <tbody> <tmpl_loop rows> <tr> <tmpl_loop cols> <td><tmpl_var data></td> </tmpl_loop> </tr> </tmpl_loop> </tbody> </table> </body> </html>

In reply to Dynamic HTML::Template Database Template by jeffa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.