While Loop - Alternate Table

I have a while loop such as below:

while($row = mysql_fetch_array($result))
{
echo “<tr>”;
echo “<td bgcolor=“first color”>” . $row[‘FirstName’] . “</td>”;
echo “</tr>”;
}

I’m wanting for it to automatically switch between:

echo “<td bgcolor=“first color”>” . $row[‘FirstName’] . “</td>”;

and

echo “<td bgcolor=“second color”>” . $row[‘FirstName’] . “</td>”;

after it pulls each row from the table. So that each row in the table has an alternating background color.

Any ideas on the best way to do this?

I personally used jQuery for this:


function alternateBG()
                {
		$("#tablediv table:even").css("background-color", "#eeeeee");
                }

However if you aren’t using jQuery I’m sure some other brilliant minds out here could help you achieve this.

On second thought, you could do something like this:


<?php

$firstColor  = "#ffffff"; // White
$secondColor = "#cccccc"; // Gray

while($row = mysql_fetch_array($result))
{
	$i=0;
	$colorThisTime = ($i/2==0) ? $firstColor : $secondColor;
echo "<tr>";
echo "<td bgcolor="$colorThisTime">" . $row['FirstName'] . "</td>";
echo "</tr>";
$i++;
}

Ideally you’d just add an alternating CSS class and style away, here’s a quick (does the job) solution.

You can do this a million ways, this is just one.


<?php
function get_color($colors = array('red', 'green')){
  static $switch = false;
  $switch = $switch ? false : true ;
  return $switch ? $colors[0] : $colors[1] ;
}

while($row = mysql_fetch_assoc($res)){
  echo '<td bgcolor="' . get_color() . '">' . $row['FirstName'] . '</td>'; 
}

So that each row in the table has an alternating background color.

Personally I wouldn’t use javascript because it won’t work in javascript disabled browsers. I would do something like this using css without styling inline.

<?php

$bgClass = 'even';

echo '<table>';
while ($row = mysql_fetch_assoc($rs)) {
    $bgClass = ($bgClass == 'even') ? 'odd' : 'even';
    echo '<tr class="' . $bgClass . '">';
    echo '<td>' . $row['col1'] . '</td>';
    echo '<td>' . $row['col2'] . '</td>';
    echo '<td>' . $row['col3'] . '</td>';
    echo '</tr>';
}
echo '</table>';
?>

and the css would be

.odd {
     background-color: green;
}

.even {
     background-color: blue;
}

I tried the following but got the below error:

PHP Fatal error: Cannot redeclare get_color()

The error message is telling you exactly what your problem is and so the fix should be obvious.

But the css solution will work as well and it’s less php code :slight_smile:

There’s no need to style the odd and even rows. One can just be the default, table colour, and only the others need to have a class applied to change them. Reduces the amount of HTML

Reduces the amount of HTML

In the code I posted there is no extraneous html elements

I think cranial-bore was implying you just add an odd OR even class attribute instead of both.

Oh ok, fair enough :slight_smile:

Yes, no problem doing it that way either. I just posted the way I normally do it which gives me a little more flexibility regarding colour choices for default table colours and colours for alternating rows.

Simplest way I can think of to do it is:

$rownum = 1;
while($row = mysql_fetch_assoc($res)){
  if ($rownum%2)
    echo '<td class="even">' . $row['FirstName'] . '</td>';
  else 
    echo '<td>' . $row['FirstName'] . '</td>'; 
  $rownum++;
}

But isn’t that giving each alternate cell a different colour which is not what the op wanted?

And what if each row has say 4 cells in it for example?

I was just including the <td> that the recent posts referenced - overlooked the extra code in the earlier posts. To get it to do the entire row regardless of how many cells are in the row you’d just apply the class to the <tr> instead of the <td>.

So it would be:

$rownum = 1;
while($row = mysql_fetch_assoc($res)){
  if ($rownum%2) echo '<tr class="even">';
  else echo '<tr>';
  echo '<td>' . $row['FirstName'] . '</td></tr>'; 
  $rownum++;
}

Then you can add as many additional <td> inside each row as you like.

So a condensed version of what was posted earlier is essentially the same.

<?php 
$bgClass = 'even'; 
echo '<table>'; 
while ($row = mysql_fetch_assoc($rs)) { 
    $bgClass = ($bgClass == 'even') ? 'odd' : 'even'; 
    echo '<tr class="' . $bgClass . '">'; 
    echo '<td>' . $row['col1'] . '</td>'; 
    echo '</tr>'; 
} 
echo '</table>'; 
?>

You can then add as many td’s as you like and make either the odd or even class a default table row colour if you like as well.

Also imv you should use the {} in IF blocks even when they are not mandatory. It’s good coding practice and noobies are less likely to get into problems in the future if they add more lines to an IF branch when there were no {} originally.

Another approach without any modulus math:


<?php
$counter = false;
foreach(range(1,10) as $item) {
	printf(
		'<p>%u is %s</p>'
		,$item
		,($counter = !$counter)?'odd':'even'
	);
}
?>