2013-05-10 63 views
1

我有一個Perl CGI腳本。我想提出基於從一個簡單的HTML表單查詢信息的動態,適當大小的表:http://jsfiddle.net/wBgBZ/4/。我想使用HTML::Table,但服務器沒有安裝模塊。管理員也不會安裝它。因此,我必須以舊時尚的方式去做。如何創建動態大小的HTML表格?

這是我到目前爲止。

#!/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

應該產生類似於以下

所需的輸出

http://jsfiddle.net/PrLvU/


東西CGI

如果用戶發只是Marshall;Tommy,表格應該是3x2。

它不起作用。我需要一種方法來動態地添加行到表。

+0

你究竟由3x2的是什麼意思? – hwnd 2013-05-11 00:37:16

+0

@JasonGray 3行2列。標題爲'Student |屬性列表「被視爲一行。 – cooldood3490 2013-05-11 00:40:10

回答

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); 
+0

它真棒!謝謝 – cooldood3490 2013-05-11 03:18:40