Tiny problem with alternating row colours

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Czar
Forum Commoner
Posts: 58
Joined: Sun Dec 29, 2002 11:17 am

Tiny problem with alternating row colours

Post 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";
						}
  }
?>
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

'K 2 problems....

Your missing a } ad the end.

And you are echo'ing the colour before setting it.
Czar
Forum Commoner
Posts: 58
Joined: Sun Dec 29, 2002 11:17 am

Post 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 :)
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

you can do this with less code:

if ($i%2==0) $color="blue"; else $color="red";
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply