Gossamer Forum
Home : General : Perl Programming :

alternating row color in tables

Quote Reply
alternating row color in tables
Can someone help me with this code segment... I have a cgi that is outputting a text db file onto a web page. I would like to have the output color alternate for each row just like the items here in gossamer forum. I understand how it works, just not how to code the loop. So what I am left with is this as an output:

http://www.overnightprofits.com/...-bin/picks/picks.cgi

Here is what I would like the output to be:

http://www.overnightprofits.com/investing/picks.htm

I have searched this forum for answers, but I could only find the discusion by alex when a user suggested this enhancement for GT FORUM - so his code is more GT FORUM specific.

Any help is GREATLY appreciated!! Thanks
Quote Reply
Re: [shiner] alternating row color in tables In reply to
Code:
my $cell_a = "white";
my $cell_b = "blue";
my @foo = (qw(perl java html php tcl));
print qq|<table>|;
for (0..$#foo) {
my $color = $_ % 2 ? $cell_a : $cell_b;
print qq|<tr><td bgcolor="$color">$foo[$_]</td></tr>|;
}
print qq|</table>|;

--Philip
Links 2.0 moderator
Quote Reply
Re: [sponge] alternating row color in tables In reply to
Sponge, Thanks for the tip. I ended up taking a slightly different route, but based it on your syntax. Thanks - for a newbie to cgi - this is a monumental breakthrough for me!

The Problem is solved:

http://www.overnightprofits.com/...-bin/picks/picks.cgi

#Builds Data Portion of the table
#
#
#

$total = 0;
$rowcount = 0;

$startVal = $in{'start'} - 1;
$endVal = $startVal + $numOfRecordsPerPage;
$ranking = $in{'start'};
$a = 0;

open (DATABASE, "$db");

while (<DATABASE>)
{
if (($rowcount >= $startVal) && ($rowcount < $endVal)) {
$row = $_;
chop $row;
@fields = split (/\|/, $row);
$rowcolor = ($a++ & 1) ? '#ccccff' : '#ffffff';
print "<tr bgcolor='$rowcolor'>\n";


for ($i = 0; $i < $numOfFields; $i++) {
print"<td align=middle><font size=$fontsize>$fields[$i]</font></td>\n";
}
print"</tr>\n";
$ranking++;
}
$rowcount++;
$total++;
}
close(DATABASE);
print "</table>\n";

print <<EOL;
<table border=0 cellpadding=$cellpadding cellspacing=$cellspacing width=$tablewidth>
<tr>
EOL