Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Table background colour to alternate cgi script

Status
Not open for further replies.

madaxe2

Technical User
Jan 23, 2005
43
US
I have preset "my $backcol = #262525; and my $colindex = 1;"
There is something wrong with my syntax but i cant work it out


MADAXE


Code:
	foreach my $i (@data) {
   	chomp($i);
   	my ($TTITLE2,$NAME,$DATE,$POST) = split(/\|/,$i);



$backcol == #262525 if ($colindex eq 1);
$backcol == #525262 if ($colindex eq 2);



print qq(<tr> 
		
	
		<th width="25%" nowrap="nowrap"><FONT COLOR=#DCD9D9" SIZE="4">$NAME</font></th>
		<th width="50%" nowrap="nowrap">-----</th>
		<th width="25%" nowrap="nowrap"><FONT COLOR=#DCD9D9" SIZE="4">$DATE</font></th>

	</tr>

	<tr>

		<th bgcolor="$backcol" colspan="100%"><FONT COLOR=#ffffff" SIZE="4">$POST</font></th>
	
	</tr>
	
\n);

$colindex += 1 if ($colindex eq 1);
$colindex -= 1 if ($colindex eq 2);

}
 
OK I added "" around the #252626 and the cgi runs now but the colours dont change in the background of the table


MADAXE
 
OK IM BEING THICK the last two if statement are fighting with each other

if $colindex = 1 the it adds 1 so now $colindex = 2


but the next line says that

if $colindex = 2 the it subtracts 1 so now $colindex = 1

so $colindex will always equal 1 thus my colour stays the same any body got a way round this


MADAXE
 
FIXED IT

Code:
	foreach my $i (@data) {
   	chomp($i);
   	my ($TTITLE2,$NAME,$DATE,$POST) = split(/\|/,$i);



$backcol = "#898181" if ($colindex eq 1);
$backcol = "#863838" if ($colindex eq 2);



print qq(<tr> 
		
	
		<th width="25%" nowrap="nowrap"><FONT COLOR=#DCD9D9" SIZE="4">$NAME</font></th>
		<th width="50%" nowrap="nowrap">-----</th>
		<th width="25%" nowrap="nowrap"><FONT COLOR=#DCD9D9" SIZE="4">$DATE</font></th>

	</tr>

	<tr>

		<th bgcolor="$backcol" colspan="100%"><FONT COLOR=#ffffff" SIZE="4">$POST</font></th>
	
	</tr>
	
\n);

		if ($colindex eq 1) {

			$colindex += 1
		}

			else {

			$colindex -= 1
		}



}
 
The modulus operator % is always useful in this kind of situation.
Code:
use strict;
use warnings;
my @data = (<DATA>);
my @backcols = ("#262525", "#525262");
chomp @data;

for (my $i = 0; $i < @data; $i++) {
    my ($TTITLE2,$NAME,$DATE,$POST) = split(/\|/,$data[$i]);
    print qq(<tr>         
        <th width="25%" nowrap="nowrap"><FONT COLOR="#DCD9D9" SIZE="4">$NAME</font></th>
        <th width="50%" nowrap="nowrap">-----</th>
        <th width="25%" nowrap="nowrap"><FONT COLOR="#DCD9D9" SIZE="4">$DATE</font></th>
    </tr>
    <tr>
        <th bgcolor="$backcols[[red]$i % 2[/red]]" colspan="100%"><FONT COLOR="#ffffff" SIZE="4">$POST</font></th>
    </tr>
\n);
}
__DATA__
one|steve|today|stuff 1
two|steve|today|stuff 2
three|steve|today|stuff 3
here it returns either 0 or 1 and is used to index the $backcols array.

NB. on your original, you had the leading " missing on all the COLOR= values, which might cause you other problemms too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top