PreshBlog

HTML::Table::FromDatabase released

by on Aug.20, 2008, under Perl, Programming

I’ve just released a new Perl module to CPAN, HTML::Table::FromDatabase. It’ll hit CPAN mirrors soon.

It subclasses HTML::Table, and its purpose is to simplify a fairly common task, of taking data from a database query and displaying it in a HTML table.

I quite often find myself writing reporting-type scripts which do a database query, then want to present that information in a table. Generating the table yourself isn’t hard, but it’s annoying and clutters your code with HTML tags. I started using HTML::Table to make this a lot easier, but you still need some code to take the data and set up the table with it.

HTML::Table::FromDatabase makes it as easy as passing the executed statement handle to the constructor, which will give you a back an HTML::Table object ready to print:

# Standard DBI stuff (yes, in the real world there'd be error-checking)
my $dbh = DBI->connect('dbi:mysql:test');
my $sth = $dbh->prepare('select * from mytable');
$sth->execute();

my $table = HTML::Table::FromDatabase->new( -sth => $sth, -border => 1 ); 

When creating a HTML::Table::FromDatabase object, all the standard HTML::Table options can still be used, there’s just one extra mandatory option, -sth, which must be the statement handle to use.

Hope it’s of use to someone :)


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!