NAME

HTML::Table::FromDatabase - subclass of HTML::Table to generate tables easily from a database query

SYNOPSIS

my $sth = $dbh->prepare('select * from my_table')
   or die "Failed to prepare query - " . $dbh->errstr;
$sth->execute() or die "Failed to execute query - " . $dbh->errstr;

my $table = HTML::Table::FromDatabase->new( -sth => $sth );
$table->print;

DESCRIPTION

Subclasses HTML::Table, providing a quick and easy way to produce HTML tables from the result of a database query.

I often find myself writing scripts which fetch data from a database and present it in a HTML table; often resulting in pointlessly repeated code to take the results and turn them into a table.

HTML::Table itself helps here, but this module makes it even simpler.

Row headings are taken from the field names returned by the query.

All options you pass to the constructor will be passed through to HTML::Table, so you can use all the usual HTML::Table features.

INTERFACE

new

Constructor method - consult HTML::Table's documentation, the only difference here is the addition of the required -sth parameter which should be a DBI statement handle, and the optional -callbacks parameter which specifies callbacks/transformations which should be applied as the table is built up (see the callbacks section below).

CALLBACKS

You can pass an arrayref of hashrefs describing callbacks to be performed as the table is built up, which can modify the data before the table is produced.

This can be very useful; one example use-case would be turning the values in a column which contains URLs into clickable links:

my $table = HTML::Table::FromDatabase->new(
   -sth => $sth,
   -callbacks => [
       {
           column => 'url',
           transform => sub { $_ = shift; qq[<a href="$_">$_</a>]; },
       },
   ],
);

You can match against the column name using a key named column in the hashref (as illustrated above) or against the actual value using a key named value.

You can pass a straight scalar to compare against, a regex (using qr//), or a coderef which will be executed to determine if it matches.

Another example - displaying all numbers to two decimal points:

my $table = HTML::Table::FromDatabase->new(
   -sth => $sth,
   -callbacks => [
       {
           value => qr/\d+/,
           transform => sub { return sprintf '%.2f', shift },
       },
   ],
);

It is hoped that this facility will allow the easyness of quickly creating a table to still be retained, even when you need to do things with the data rather than just displaying it exactly as it comes out of the database. =head1 AUTHOR

David Precious, <davidp@preshweb.co.uk>

Feel free to contact me if you have any comments, suggestions or bugs to report.

COPYRIGHT AND LICENSE

Copyright (C) 2008 by David Precious

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.