开发者

How can I dynamically generate an HTML tables containing 100 rows each in Perl?

use POSIX;
my $test = "";
my $elements = scalar(@array);
my $tablecount = ($elements / 100);
my $tblnum = ceil($tablecount);
my @hundred;
 foreach $test (@array) {
   until ($tblcnt == $tblnum){
   @hundred(@array, 0, 99);
   print qq~<table id="$tblcnt"><tr><td>~;
     foreach $test (@hundred){
     print qq~<tr><td>$test</td></tr>~;
     }
   print qq~</table>~;
   $tblcnt++;
   }
}

UG!!! I am trying to learn this but, I just cannot get it right!!!

I am trying to dynamically generate "x" number of html tables filled with up to 100 lines of data each.

I can get the table count needed, I can loop, I can add but, one thing is for sure: I CANNOT WRITE CODE.

Here is the result wanted:

1- I have 1027 lines of data from an array.

2- I want my script to make 11 html tables with up to 100 lines each. ((Layers actually) which by default will be not visible via css. That way I can do some show hide on the tables like a "next prev" navigation. I don't need help with the cross browser css.)

3- IF there is a better way, a method that I can comprehend anyhow, other than using visible= method, please elaborate!

I gave up trying to have Perl make pages of 100 with "next prev" links to the data so I resorted to using css javascript onclick=yadayada to "display the data".

I thought it would be easier to shift off 100 lines of the array in a loop and put the lines in their own html tables. Not.

I have failed miserably and need assi开发者_StackOverflowstance.


I think you need to spend more time learning the basics of Perl and CGI before writing any scripts.

It is useful to separate logic from presentation in CGI scripts. To that end, I find HTML::Template very useful.

The following script will generate an HTML document containing 100 tables with 100 rows of 10 cells each. It will take its sweet time doing that.

#!/usr/bin/perl

use strict; use warnings;

use CGI::Simple;
use HTML::Template;

my $tmpl = HTML::Template->new(scalarref => page_template() );

my @tables;

for my $t (1 .. 100) {
    my @rows;
    for my $r (1 .. 100) {
        push @rows, { CELLS => [ map { CELL => $_ }, 1 .. 10 ] };
    }
    push @tables, { ID => "table_$t", ROWS => \@rows }
}

$tmpl->param(TABLES => \@tables);

my $cgi = CGI::Simple->new;

print $cgi->header('text/html');
$tmpl->output(print_to => \*STDOUT);

sub page_template {
    return \ <<EO_TMPL
<!DOCTYPE HTML>
<html>
<head><title>Tables Example</title></head>
<body>
<TMPL_LOOP TABLES>
<table id="<TMPL_VAR ID>">
<TMPL_LOOP ROWS>
<tr>
<TMPL_LOOP CELLS>
<td><TMPL_VAR CELL></td>
</TMPL_LOOP>
</tr>
</TMPL_LOOP>
</table>
</TMPL_LOOP>
</body>
</html>
EO_TMPL
    ;
}


my $cnt = 0;

while (@array) {
    my @rows = splice @array, 0, 100;
    print qq(<table id="t$cnt">\n);
    for my $row (@rows) {
        print "<tr><td>$row</td></tr>\n";
    }
    print "</table>\n";
    ++$cnt;
}

You may want to use HTML::Table for generating HTML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜