如何创建动态大小的HTML表?

时间:2013-05-10 23:19:32

标签: html perl functional-programming cgi

我有一个Perl CGI脚本。我想基于来自简单HTML表单的查询信息制作一个动态的,适当大小的表:http://jsfiddle.net/wBgBZ/4/。我想使用HTML::Table,但服务器没有安装模块。管理员也不会安装它。因此,我必须采用旧时尚的方式。

这是我到目前为止所拥有的。

#!/usr/bin/perl
use strict; use warnings;
use CGI qw( :standard);

print header;
print start_html(
    -title => 'Creating Tables'
);

# Process an HTTP request
my $query = param("names");
my @students_in_class = split(/;/, $query);
my %attributes = (
    'Tommy'   => 'A star baseball player who has lots of potential to play in the Major League of Baseball. ',
    'Tyrone'  => 'An honor roll athlete. His father is really proud of him. When he graduates, he wents to work at the National Institute for Public Health. His father wants him to become a doctor but he wants to pursue Physics.',
    'Marshall' => 'A professional WWE wrestler.',
);

print table({-border=> undef},
    caption('Students in the class'),
        Tr({-align=>'CENTER',-valign=>'TOP'},
            [ 
             th(['Student', 'List of Attributes']),
             foreach (@students_in_class){       # !!!!! problem line !!!!!!
                 td(['$_' , '$attributes{$}']),
             }
             ]
           )
 );

如果用户在搜索栏中输入以下内容:Tyrone;Tommy;Marshall

CGI应该产生类似于以下内容的内容

期望输出

http://jsfiddle.net/PrLvU/


如果用户只输入Marshall;Tommy,则表格应为3x2。

它不起作用。我需要一种方法来动态地将添加到表中。

1 个答案:

答案 0 :(得分:1)

这是未经测试的,但我认为这是你想要的。您可能需要将某些表属性更改为所需的需求。

use strict; 
use warnings;
use CGI qw( :standard );

print header,
      start_html(-title => 'Creating Tables');

my $query = param('names');

my @headers;
my @students = split(/;/, $query);

my %attributes = (
        Tommy   => 'A star baseball player.',
        Tyrone  => 'An honor roll athlete.',
       Marshall => 'A professional WWE wrestler.',
);

$headers[0] = Tr(th('Student'), th('List of Attributes'));

for my $i (@students) {
  push @headers, Tr( td($i), td($attributes{$i}));
}

print table( {-border => undef}, @headers );