Jump to content

[SOLVED] Switch TR color in a while loop by evens or odds


iceman023

Recommended Posts

I have a table that is in a while loop

 

$numrowsC = mysql_num_rows($newStatement); while ($newData = mysql_fetch_array($newStatement))
{
	echo "<table width='864' border='5' align='center' bgcolor='#F4F4F4' bordercolor='#000000' fontcolor='#000000' cellspacing='5' cellpadding='5'>";
	echo "<tr>";

 

is there anyway i can add a switch within the loop and change the background color of the rows... maybe something like IF odd number from $numrowsC it will be blue and if even it will be red.. something like that...

Link to comment
Share on other sites

it didnt work but i may have entered it wrong.. im kinda new to php..

 

 

<?php
while ($newData = mysql_fetch_array($newStatement))
{
$loop = 1;
	echo "<table width='864' border='5' align='center' bgcolor='#F4F4F4' bordercolor='#000000' fontcolor='#000000' cellspacing='5' cellpadding='5'>";
	echo "<tr>";
	echo "<td>";
	echo "<h3>";
	echo "<br />";
	echo "<font size='3' color='ff00d2'>";
	echo "&nbsp &nbsp Commented By: &nbsp &nbsp";
	echo $newData['Author'];
	echo "</font>";
	echo "<ul>";
	echo "<li>";
	echo $newData['Comments']; //the actual text of the comment
	echo "</li>";
	echo "</ul>";
$loop++;
if ($loop % 2) $trcolor = 'blue';
  	else $trcolor = 'red';
?>

Link to comment
Share on other sites

yeh u did it wrong lol

 

<?php
   $loop = 1;
     echo "<table width='864' border='5' align='center' bgcolor='#F4F4F4' bordercolor='#000000' fontcolor='#000000' cellspacing='5' cellpadding='5'>";
while ($newData = mysql_fetch_array($newStatement))
   {
    $loop++;
    if ($loop % 2) $trcolor = 'blue';
     else $trcolor = 'red';
      echo "<tr bgcolor='$trcolor'>";
      echo "<td>";
      echo "<h3>";
      echo "<br />";
      echo "<font size='3' color='ff00d2'>";
      echo "&nbsp &nbsp Commented By: &nbsp &nbsp";
      echo $newData['Author'];
      echo "</font>";
      echo "<ul>";
      echo "<li>";
      echo $newData['Comments']; //the actual text of the comment
      echo "</li>";
      echo "</ul>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.