Perl – How to display data in table with Perl

html-tableperl

I am getting data from a database. It has 4 rows and 6 columns. I want to show this in an HTML table but am having problems. I've made the TH for the 6 columns. However, I am having problems in displaying the 4 rows. This is what I have so far:

while (($f1, $t2, $n3, $k4, $d5, $e6) = $sth1->fetchrow_array)
{
        push (@first, $f1);
        push (@second, $t2);
        push (@third, $n3);
        push (@fourth, $k4);
        push (@fifth, $d5);
        push (@sixth, $e6);
}

@first, @second…and others are arrays

While dsplaying data if I do:

foreach (@first)
{
        print "<td>$_</td>";
}

That is displaying data vertically but I want it to display horizontally.

Best Answer

You could also use HTML::Table.

use strict;
use warnings;
use HTML::Table;

my $table = HTML::Table->new( 
    -cols    => 6, 
    -border  => 1,
    -padding => 1,
    -head => [qw( f1 t2 n3 k4 d5 e6 )],
);

# do query here.    

while ( my @row = $sth->fetchrow_array ) {
    $table->addRow(@row);
}

$table->print;