如何使用Perl在表格中显示数据? [英] How can I display data in table with Perl?

查看:150
本文介绍了如何使用Perl在表格中显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中获取数据。它有4行6列。我想在HTML表格中显示这个但是遇到了问题。我为6列制作了TH。但是,我在显示4行时遇到问题。这是我到目前为止:

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 ...等等是阵列

@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.

推荐答案

问题中的代码是使用Perl的DBI(数据库接口)编写的:

The code in the question is written using Perl's DBI (DataBase Interface):

print "<table>\n";
while (($f1, $t2, $n3, $k4, $d5, $e6) = $sth1->fetchrow_array)
{
    print "  <tr>\n";
    print "    <td>$f1</td>\n";
    print "    <td>$t2</td>\n";
    print "    <td>$n3</td>\n";
    print "    <td>$k4</td>\n";
    print "    <td>$d5</td>\n";
    print "    <td>$e6</td>\n";
    print " </tr>\n";
}
print "</table>\n";

或者您可以将行读入数组,然后更简洁地打印数组:

Or you could read the row into an array and then print the array more succinctly:

print "<table>\n";
while (my(@row) = $sth1->fetchrow_array)
{
    print "  <tr>\n";
    print "    <td>$val</td>\n" foreach my $val (@row);
    print " </tr>\n";
}
print "</table>\n";

这篇关于如何使用Perl在表格中显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆