Perl CGI Programming
HTML Elements: Tables
- The <TABLE> tag allows you to create a spreadsheet like
table.
- Within this table, you specify the table caption with the
<CAPTION> construct.
- Within this table, you specify the table heading with the
<TH> construct.
- Within this table, you specify a table row with the
<TR> construct.
- Within this table, you specify a table data with the
<TD> construct.
- The raw HTML to generate a table would be:
<TABLE>
<CAPTION>Compensation Packages</CAPTION>
<TR><TH>Position</TH> <TH>Compensation</TH> <TH>Vacation</TH></TR>
<TR><TD>Programmer</TD> <TD>$35,000</TD> <TD>2 weeks</TD></TR>
<TR><TD>Sales</TD> <TD>$250,000</TD> <TD>4 weeks</TD></TR>
<TR><TD>CEO</TD> <TD>$750,000</TD> <TD>9 weeks</TD></TR>
</TABLE>
<HR>
The Perl CGI construct for creating a table would be:
# Try a table
my @myHeading = ['Position', 'Compensation', 'Vacation'];
my @myRowOne = ['Programmer', '$35,000', '2 weeks'];
my @myRowTwo = ['Sales', '$250,000', '4 weeks'];
my @myRowThree = ['CEO', '$750,000', '9 weeks'];
print $query->hr();
print "This should be a table\n";
print $query->table($query->caption('Compensation Packages'),
$query->Tr( [ $query->th(@myHeading),
$query->td(@myRowOne),
$query->td(@myRowTwo),
$query->td(@myRowThree)] ));
print $query->hr();
It's not as bad as it looks.
Note that the "TR" construct had to be written as "Tr()" for
PerlCGI (as opposed to "tr()") because "tr()" was an already existing
Perl function.
Back to Syllabus
Previous: HTML Elements: Formatted Lists
Next: HTML Elements: Exercise