How can I change a rowspan attribute within my CGI program?
I am writing a CGI script that will process form data, and it should print the name of the input, along with its value, in a table. When I have one or more values with the same name, the name should span rows to accommodate all values that correspond to that name. For example, if I have a name "color" with its values at "red", "green", "blue", then color should span 3 rows in my table. My question is, how would i change the rowspan attribute in my script to a开发者_如何学运维ccommodate this:
#!/usr/bin/perl --
use strict;
use CGI;
print <<HTTP;
Status: 200 OK
Content-Type: text/html
HTTP
print <<HTML;
<html>
<head>
<title>Parameters<title>
<head>
<body>
<table border="1" cellpadding="5" cellspacing="1">
<tr>
<th>Name</th>
<th>Value</th>
</tr>
HTML
my $query = new CGI;
my($name, $value);
foreach $name ( $query->param)
{
print "<tr>";
print "<td>$name</td>";
foreach $value($query->param($name))
{
print "<td>$value</td>";
print "</tr>";
}
}
Try this:
my $query = new CGI;
my($name, $value);
foreach $name ($query->param) {
my @values = $query->param($name);
my $count = @values;
print "<tr>";
print "<td rowspan='$count'>$name</td>";
print "<td>".shift(@values)."</td>";
print "</tr>";
foreach $value (@values) {
print "<tr>";
print "<td>$value</td>";
print "</tr>";
}
}
BTW, I would suggest you to consider using some template processing system, e.g. Template Toolkit.
精彩评论