sir_com has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a CGI script where I am reading data from a text file (have a text file with lots of data) and processing them. I want to display them in row,column fashion or tabular fashion in the page, but the table will have 24 columns and dynamic rows. How can I generate a dynamic table in CGI? I tried HTML::Table module but the generated table is not getting desplayed. Please help me?

Thanks, sir_com

Replies are listed 'Best First'.
Re: CGI problem help
by ikegami (Patriarch) on Sep 11, 2009 at 20:30 UTC

    How can I generate a dynamic table in CGI?

    You answered yourself in the next sentence.

    I tried HTML::Table module but the generated table is not getting desplayed.

    That sucks. Maybe you should ask PerlMonks for help with your problem using HTML::Table.

    (I'm not sure what you're expecting besides our sympathies since you didn't tell us anything about your problem.)

Re: CGI problem help
by graff (Chancellor) on Sep 11, 2009 at 22:52 UTC
    You should be able to run your script from a shell command line, and it will print its HTML output to your shell terminal (STDOUT). Check the section of the CGI man page about "DEBUGGING".

    If running the script at the command line causes an error instead of HTML output, the error message will tell you what's wrong and which line number(s) you have to fix in your script. If you still need our help at that point, you'll need to show us the error message, the relevant parts of your code, and maybe a small sample of data.

Re: CGI problem help
by jrsimmon (Hermit) on Sep 11, 2009 at 21:42 UTC

    What markup is your script creating when using HTML::Table? As ikegami cheekily suggests, perhaps looking for an alternative solution is a bit premature?

    That said, printing the markup for a fixed-column table is pretty simple, especially if you use a template like HTML::Template.

Re: CGI problem help
by CountZero (Bishop) on Sep 12, 2009 at 06:24 UTC
    but the generated table is not getting desplayed
    Is anything else being displayed? Did you check the logs to see if an error was logged?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Hi Monks,

      Thanks for your reply. I did checked again and corrected few things and the table is created now. But I am facing a issue.

      I am reading data from a file, processing it and then trying to put the data in table accordingly. Table has 40 rows and 3 columns. Below is the code:

      #!C:\perl\bin\perl.exe use strict; use CGI; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use HTML::Table; my $linecount=0; open(FILE,"details.txt")or die("Sorry can't open $!\n"); my $cgi=CGI->new; print $cgi->header; print $cgi->start_html("Test display page"); print $cgi->h3("Testing the output format"); my $table = new HTML::Table(-rows=>40, -cols=>3, -align=>'left', -border=>1, -width=>'30%', -padding=>5, -head=> ['col1','col2','col3']); OUTER: while(my $line = <FILE>) { chomp($line); my @field=split(/\s+/,$line); for(my $j=1;$j<=24;$j++) { $table->setCell($linecount, $j, $field[($j-1)]); } $linecount++; } close(FILE); print $table->getTable; print $cgi->end_html;

      It's printing the columns but data's are not coming somehow. When run the code without html it displays the data. Please help to know where I am going wrong.

      Thanks, sir_com
        I'm still guessing, but most likely your error is in:

        • my @field=split(/\s+/,$line); and split does not return the results you expect; or
        • there is something wrong with your for loop. You seem to expect 24 elements in each line of your file and you store these in @field but yet your table constructor is called with 3 rows as a parameter only. I know that HTML::Table will add extra cells as and when necessary, but still it is a bit strange.
        Have you tried adding a whole row at a time?
        $table->addRow(@field);
        Then all you need is the while loop, it replaces the whole for loop.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James