creating html table using CGI.pm

Hi there,

I am using CGI.pm with perl 5.004 for my CGI program. I am having some problem for writing

<TABLE BORDER="0" WIDTH="800">
      <TR>
            <TD WIDTH="160">&nbsp;</TD>
            <TD WIDTH="208">&nbsp;</TD>
            <TD WIDTH="160">&nbsp;</TD>
            <TD WIDTH="272">
            <B>Selected Analysts</B>
            </TD>
      </TR> </TABLE>

using table, Tr and td functions of CGI object. I checked some of the sample programs. But I could not find any example explaining how to create a table with different column widths as I require. I tried some stuff, but it gives compilation errors which are very ambigious.
I would appreciate if anybody can give some pointers.

Maha


mahaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ozoConnect With a Mentor Commented:
print table({border=>0,width=>800},"\n",
    Tr( "\n",
        (map{td({width=>$_},"&nbsp;"),"\n"} (160,208,160)),
        td({width=>272},"\n",
            b("Selected Analysts"),"\n"
        ),"\n"
    )
);
0
 
maneshrCommented:
do you want to print the tables using the CGI.pm functions only?

you can use the print statement for the same without having to worry about errors..

Eg.

print qq{
<TABLE BORDER="0" WIDTH="800">
                   <TR>
                   <TD WIDTH="160">&nbsp;</TD>
                   <TD WIDTH="208">&nbsp;</TD>
                   <TD WIDTH="160">&nbsp;</TD>
                   <TD WIDTH="272">
                   <B>Selected Analysts</B>
                   </TD>
                   </TR> </TABLE>

};

now you dont have to worry about escaping the quotes too. just put the code as if you were writing a plain HTML file!!

0
 
mahaAuthor Commented:
Hi there,

Yes that is true;

currently I am doing like

print <<EOF;

<TABLE BORDER="0" WIDTH="800">
                   <TR>
                   <TD WIDTH="160">&nbsp;</TD>
                   <TD WIDTH="208">&nbsp;</TD>
                   <TD WIDTH="160">&nbsp;</TD>
                   <TD WIDTH="272">
                   <B>Selected Analysts</B>
                   </TD>
                   </TR> </TABLE>
EOF


It works fine. But it is static data. My table rows will be dynamically generated based on database rows.  I can use multiple prints to create table. But I just want an elegant mechanism to do it. table, Tr and td provides that, but i could not be able to set different column widths.

Regards,
Maha
0
 
maneshrCommented:
but would you sacrifice core functionality for elegance??
0
 
moonlxCommented:
use CGI;
my $query = new CGI;
print $query->header();
print "<HTML><HEAD></HEAD><BODY>";
#create your headings for your table
print "<TABLE BORDER=1 WIDTH=800>
<TR>
<TD WIDTH=160>&nbsp;</TD>
<TD WIDTH=208>&nbsp;</TD>
<TD WIDTH=160>&nbsp;</TD>
<TD WIDTH=272>
<B>Selected Analysts</B>
</TD>
</TR>";

# your select statement - whatever #method you are using, for example
$res = executeQuery("SELECT * FROM table");      
# fetch the rows (1 at a time), the #while condition is my method of the #rows, substitute with your own method

while($Data = fetchRow($res))
{
#dynamically display the data, the #getItem is my method of getting the #values in the database columns, #substitute this for whatever method #you are using

print" <TR><TD WIDTH=160>".getItem($Data,"column1")."</TD><TD WIDTH=208>".getItem($Data,"column2")."</TD><TD WIDTH=160>".getItem($Data,"column3")."</TD><TD WIDTH=272><FONT class=Label>".getItem($Data,"column4")."</TD></TR>";
}
                  
print"</TABLE></DIR></BODY></HTML>";
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.