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 $_ : ' ' }
} @$_
] }} @$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>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|