Page 1 of 1

Tiny problem with alternating row colours

Posted: Fri Jun 13, 2003 6:49 am
by Czar
Everything is ok, but the first row that script prints after "headlines". The bgcolor of first row prints RED (?). Why.

Here's the code:

Code: Select all

<?php
$query = mysql_query($q) or die(mysql_error());
echo "<table cellpadding=5 border=0><tr><td bgcolor="#cccccc" class=main><a href=wpn.php?cat=$cat&orderBy=man>order by manufacturer</a></td><td bgcolor="#cccccc" class=main><a href=wpn.php?cat=$cat&orderBy=model>order by modelname</a></td></tr>";
if(mysql_num_rows($query) != 0) {
  $alter_row_colour = "1";
  for($i = mysql_num_rows($query); $i > 0; $i--) { 
  	  $ii = $i; 
 	  	$ii--;
			$row_000 = mysql_result($query,$ii,"id");
			$row_00 = mysql_result($query,$ii,"cat"); 
	  	$row_01 = mysql_result($query,$ii,"man");
		  $row_02 = mysql_result($query,$ii,"model");
		  $row_03 = mysql_result($query,$ii,"descr");
			$row_04 = mysql_result($query,$ii,"img");
			echo "<tr><td bgcolor=$colour class=main width=150><a href=wpn.php?cat=$cat&orderBy=man>$row_01</a></td><td bgcolor=$colour class=main width=150><a href=wpn.php?action=detail&cat=$row_00&id=$row_000>$row_02</a></td></tr>";
      if($alter_row_colour == "1") {
						$colour = "#e7e7e3";
						$alter_row_colour = "2";
						} else {
						$colour = "#cccccc";
						$alter_row_colour = "1";
						}
  }
?>

Posted: Fri Jun 13, 2003 6:54 am
by []InTeR[]
'K 2 problems....

Your missing a } ad the end.

And you are echo'ing the colour before setting it.

Posted: Fri Jun 13, 2003 6:59 am
by Czar
Ok, got that. Script couldn't set colour until loop started second time and threw some "default" color (red) there...

Stupid mistake... well, i'm tired and bored at work and got nothing else to do :)

Posted: Fri Jun 13, 2003 7:34 am
by Tubbietoeter
you can do this with less code:

if ($i%2==0) $color="blue"; else $color="red";

Posted: Fri Jun 13, 2003 7:56 am
by twigletmac
Tubbietoeter wrote:you can do this with less code:

if ($i%2==0) $color="blue"; else $color="red";
or

Code: Select all

$color = ($i%2 == 0) ? 'blue' : 'red';
Mac