Create a table of form fields with the use of extensively
prebuilt components.
#!/usr/bin/perl -w
use CGI;
use strict;
# Demonstration program of using arrays to build up and nest HTML
# constructs using arrays and scalars.
#
# Bill Kilgallon, 5/15/00
# Bill@KilgallonFamily.Com
# This software released via the Artistic License
#
# Create our CGI object so we can get at the methods we want.
my $query=new CGI;
# Create a header, and let it default arguments, it likely knows
# better then we do.
print $query->header();
# Start the body of our document, and give it a title
print $query->start_html({-title=>'Table Demo',
-author=>'Bill Kilgallon'});
# Declare some variables
my (@rowOneData, @rowTwoData, @rowThreeData, @rowFourData);
my ($rowOneHtml, $rowTwoHtml, $rowThreeHtml, $rowFourHtml);
my $number;
# We will start from the inside out, and build up from the simplest
# parts. These will then be gradually assembled to make the entire
# table construct.
# First, the easy part, the header row.
# Use our normal HTML generation logic, but instead of printing
# it out, save it in a scaler string variable.
my $headerHtml = $query->th(['Column One', 'Column Two', 'Column Three',
'Column Four']);
# Now, create the text entry fields for the rows. This time, we will
# use the push function to build up an array of individual form field
# strings. This will be helpfull later, as the perlCGI table
# functions are distributive. That is, they will automatically apply
# themselves to each element within an array. Again, instead of
# printing it out now, create an array for future use. We are
# building up pieces.
# Note also that we are letting the loop variable change the field
# name... in effect we are dynamically naming our variables
# (FieldOneOne, FieldOneTwo, FieldOneThree, etc).
foreach $number ("One","Two","Three","Four") {
push (@rowOneData, $query->textfield({-name=>"FieldOne$number"}));
push (@rowTwoData, $query->textfield({-name=>"FieldTwo$number"}));
push (@rowThreeData, $query->textfield({-name=>"FieldThree$number"}));
push (@rowFourData, $query->textfield({-name=>"FieldFour$number"}));
}
# Now, convert the text fields built above into table data entries.
# Because the CGI methods are distributive, we only need to call the
# method once and pass it the whole array, and the HTML | and |
# constructs will be automatically created for each element within the
# array, and the whole mess will be returned as a big long html
# string. We will save this string to be output later.
# Note we could have generated yet another array here also using push,
# and then in the section below where the Tr() constructs appear use
# the distributive proerty of that method as well, and had a lot less
# typing. Instead, I figured you are confused enough at this point,
# and opted for clarity over elegance.
$rowOneHtml = $query->td(\@rowOneData);
$rowTwoHtml = $query->td(\@rowTwoData);
$rowThreeHtml = $query->td(\@rowThreeData);
$rowFourHtml = $query->td(\@rowFourData);
# Last part to be built (and another easy one)... Build my caption
my $tableCaption = $query->caption("Bills Sample Table");
# Now, build up an entire table based on the parts assembled above
# into one big long HTML string.
my $myTable = $query->table({-border=>'5'},
$tableCaption,
$query->Tr($headerHtml),
$query->Tr($rowOneHtml),
$query->Tr($rowTwoHtml),
$query->Tr($rowThreeHtml),
$query->Tr($rowFourHtml));
# Start the form
print $query->startform({-method=>'post'});
# Dump all the previously built up HTML code to the screen.
print $myTable;
# Create and display the submit and reset buttons
print $query->submit();
print $query->reset();
# display the HTML form end construct.
print $query->endform();
# And finally, end the document.
print $query->end_html;