I've been trying to solve this for straight hours, and admittedly my knowledge of PHP is extremely limited, as this is not my code (I have hired a programmer). Basically, I'm trying to have alternating table row colors in a table on my page, with the lightest color coming first, then the darker one, then the lighter one, and it keeps repeating.

But, if there are an even number of dogs being displayed, the darker color comes first. If there's an odd number, everything goes as it should.

I've used multiple tutorials on this, but when the alternating table colors show up correctly, it just shows 5 or 6 repeats of the same dog. So I still haven't found a solution to this.

The code:

	echo ("<table style='border-style:solid; border-width:2px; border-color:#b7996d;' cellspacing='3' cellpadding='3' width='85%'>
	<tr><td bgcolor='#644a24' colspan='4' align='center'>
	<font color='d1ead0'><b>Dogs in Kennel</b></font>
	</td></tr>

<tr><td bgcolor='#afdbad' width='31%' align='center'>
<b>Name</b>
</td>

<td bgcolor='#afdbad' width='19%' align='center'>
<b>Sex</b>
</td>

<td bgcolor='#afdbad' width='28%' align='center'>
<b>Breed</b>
</td>

<td bgcolor='#afdbad' width='22%' align='center'>
<b>Age</b>
</td></tr>");

$query = ("SELECT * FROM dogs WHERE dog_owner='$cani_id' ORDER BY dog_id");
$result2 = mysql_query($query) or die(mysql_error());
$check_dogs = mysql_num_rows($result2);

if ($check_dogs > '0'){
$query = ("SELECT * FROM dogs WHERE dog_owner='$cani_id' ORDER BY dog_id");
$res = mysql_query ($query) or die(mysql_error());
while ($row = mysql_fetch_assoc ($res)){
$dog_id = $row['dog_id'];
$dog_age = $row['dog_age'];
$dog_name = $row['dog_name'];
$dog_nickn = $row['dog_nickn'];
$dog_breed = $row['dog_breed'];
$dog_owner = $row['dog_owner'];
$dog_gender = $row['dog_gender'];

if (!empty($dog_nickn)){
$dog_nickn = ("\"$dog_nickn\"");}

$check_dogs++;
if (is_float($check_dogs/2)){
$rowcolor = ("#c9e9c7");}

if (!is_float($check_dogs/2)){
$rowcolor = ("#e1f2de");}

if (($check_dogs % 2) == 0){
$hai = ("even");}
else {
$hai = ("odd");}

echo ("<tr><td bgcolor='$rowcolor' width='31%' align='center'>
<a href='/dogs.php?id=$dog_id'>$dog_name $dog_nickn</a>
</td>

<td bgcolor='$rowcolor' width='19%' align='center'>
$dog_gender
</td>

<td bgcolor='$rowcolor' width='28%' align='center'>
$dog_breed
</td>

<td bgcolor='$rowcolor' width='22%' align='center'>
$dog_years $year_count $dog_months $month_count
</td></tr>");}}

else {
echo ("<tr><td bgcolor='#e1f2de' colspan='4' align='center'>
<i>There are no dogs in this kennel.</i>
</td></tr>");}

echo ("</table><br>");

Thank you so much in advance to whoever can help me with this!

    hi
    use this one:

    <?php 
    $num=mysql_num_rows($result);
    
    if($num%2!=0)
      $an="odd";
    else
     $an="even";
    
    if($an=="even")
    {
        $dark="#000000"; //black --- use ur color
       $light="#FFFFFFF";// white --- use ur color
    }else if($an=='odd')
    {
         $dark="#FFFFFF"; //white  --- use ur color
       $light="#000000";// black --- use ur color
    
    }
    
    $i=0;
    while($aa=mysql_fetch_array($result))
    {
        $i++;
        if($i%2==0)
           $color=$dark;
       else
          $color=$light;
      // Use this $color as ur <tr bgcolor='<?php echo $color;?>'>
    }
    
    ?>
      <?php
      
      $bgColors = array(
          0 => '#ffffff',
          1 => '#bbbbbb'
      )
      
      
      $bgcolor = $bgColors[0];
      
      $ouput[] = '<table>';
      
      foreach ($results as $result):
          $bgColor = ($bgColor == $bgColors[0]) ? $bgColors[1] : $bgColors[0];
      
      $output[] = "<tr>";
      $output[] = "<td style=\"background:{$bgColor};\"> foo </td>";
      $output[] = "</tr>";
      }
      
      $output[] = '</table>';
      
      
      echo implode(PHP_EOL, $output);

        Thank you so much to everyone who replied, I was ripping my hair out over this. I ended up using devphp12's code, and it fixed my problem, so thank you so much!!

          Remember to mark this thread as resolved using the thread tools.

          That said, I still think that a client side approach is better: this functionality is more in the realm of style than server side logic.

            Write a Reply...