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

hey everyone, I have been using CGI.pm for some basic website development rececently and I now have a fairly advanced table that I would like to use for a dynamic summary page...tables within tables...stuff like that....I just can't seem to get the formatting right....one big problem is the -width=> option for td,...it seems that if I have multiple columns in the same row, I cannot set the width's for each column...anyone have any suggestions? Also, is there something I should be using rather than cgi.pm?? TIA

Replies are listed 'Best First'.
Re: Tables in CGI.pm
by Masem (Monsignor) on Jan 09, 2002 at 20:07 UTC
    The way the HTML modifiers work in CGI.pm is that there are two 'arguments' to each HTML function:
    TAG( <optional hash>, <scalar>|<array ref> )
    Assuming that the optional hash exists, then these will be used as tag attributes for the second argument, otherwise there will be no additional attributes. If the second argument is a scalar, then only one tag will be generated (with closure). On the other hand, if the second argument is an array ref, then a tag with the given attributes will be generated for each element in that array.

    For example, to print a table row where each element is the same width, you can do:

    print $cgi->table( $cgi->Tr( $cgi->td(-width=>"20%", [1,2,3,4,5]) ) );
    However, you want to change the width. Unfortunately, to do this, you can't just simply use this format. Instead, you'll have to specify each td separately:
    print $cgi->table( $cgi->Tr( [ $cgi->td(-width=>"10%", 1 ), $cgi->td(-width=>"20%", 2 ), $cgi->td(-width=>"30%", 3 ), $cgi->td(-width=>"20%", 4 ), $cgi->td(-width=>"20%", 5 ) ] ) );

    There's numerous ways to improve this code with maps and other modules, but that's the simpliest way of do this. If this code gets too complicated, you may want to look at the various template modules (HTML::Template, HTML::Mason, Template Toolkit 2), which allow you to write the HTML for the table explicitly, then insert the values from your perl code dynamically.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      Davis, That did it...thanks.... I think in the future I will use HTML::Template, just seems alot easier.

      Brett

        It would appear the code you posted has some syntax errors,
        but the problem is the fact that you're passing Tr() an array of references. Tr would produce a separate row for each element in the array you pass it.
        If you pass a single value, like so:
        #!/usr/bin/perl -w use strict; use CGI qw(:standard); print table({-border=>'1'}, Tr( td({-width=>'20%'},'col1'), td({-width=>'80%'},'col2') ) );
        You should find that solves your problem, (I've removed the square brackets that created the anonymous arrayref).
        davis
Re: Tables in CGI.pm
by davis (Vicar) on Jan 09, 2002 at 20:17 UTC
    I may be misunderstanding the question, but perhaps the following example could be of use:
    #!/usr/bin/perl -w use strict; use CGI qw(:standard); my @table_rows; push @table_rows, th({-width=>"20%"}, "Hello") . th({-width=>"20%"}, "There") . th({-width=>"60%"}, "World"); push @table_rows, th({-width=>"20%"}, "The") . th({-width=>"20%"}, "Second") . th({-width=>"60%"}, "Line"); print table(Tr(\@table_rows));
    This produces (once formatted):
    <table> <tr> <th width="20%">Hello</th> <th width="20%">There</th> <th width="60%">World</th> </tr> <tr> <th width="20%">The</th> <th width="20%">Second</th> <th width="60%">Line</th> </tr> </table>
    For this to work, you'd need to call th() for each column separately, rather than the more normal passing of an anonymous arrayref
    Hope that helps
    davis Update: Beaten (as ever :) ) by Masem. Small stupidity fix
Re: Tables in CGI.pm
by stefan k (Curate) on Jan 09, 2002 at 19:38 UTC
    Well this sounds more like a (classic) HTML problem than like a perl problem. Maybe you should consider reading the HTML table behaviour in any decent HTML book/course/whatevermedium. And if you think you got a perl problem you'd better post a (small) snippet of your code.

    it seems that if I have multiple columns in the same row, I cannot set the width's for each column

    You need to see this in the whole table. A row is not independent from the other rows, plus the setting of width of a row might not work when the content is too wide plus you might run into the browser hell...

    Sorry, if this isn't a big help, but I think your problems are with HTML and not perl.

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

(cLive ;-) Re: Tables in CGI.pm
by cLive ;-) (Prior) on Jan 10, 2002 at 10:44 UTC
    I wrote about this recently here. You might find that useful.

    cLive ;-)