Jump to content

[SOLVED] alternating bgcolor on table rows


Recommended Posts

I am having a difficult time in trying to get the alternating background color to work.

 

foreach($search_rows as $row) {
    			$numrows = count($row);
    			
    			for($i = 0; $i < $numrows; $i++) {
    				if (($i % 2) == 0) {
				 $alt = "d4e4fe";
				} else {
				 $alt = "bfd4f8";
				}
    			
    			echo '<tr bgcolor="#' . $alt . '">';
    				echo '<td>' . $row->branch_name . '</td>';
    				echo '<td>' . $row->address . '</td>';
    				echo '<td>' . $row->city . '</td>';
    				echo '<td>' . $row->state . '</td>';
    				echo '<td>' . $row->zip . '</td>';
    				echo '<td>' . format_phone($row->phone_number) . '</td>';
    			echo '</tr>';
    			}
    		}

Link to comment
Share on other sites

What you want to do is put the count outside of the loop. And I don't think $row is an array.

 

Also, I'm not sure if bgcolor works on <TR> tag. But if it does, try this -

 

$row = 0;
foreach($search_rows as $row) {
     $alt = (++$row % 2 == 0)? 'd4e4fe' : 'bfd4f8';
     echo '<tr bgcolor="#' . $alt . '">';
     echo '<td>' . $row->branch_name . '</td>';
     echo '<td>' . $row->address . '</td>';
     echo '<td>' . $row->city . '</td>';
     echo '<td>' . $row->state . '</td>';
     echo '<td>' . $row->zip . '</td>';
     echo '<td>' . format_phone($row->phone_number) . '</td>';
     echo '</tr>';
}

Link to comment
Share on other sites

Hmmmm.... all that did was do the same thing it has been doing and set the whole table to one color.

 

updated code:

 

$row = 0;
	foreach($search_rows as $row) {
		$alt = ($row % 2 == 0)? 'd4e4fe' : 'bfd4f8';
			echo '<tr>';
			echo '<td bgcolor="#' . $alt . '">' . $row->branch_name . '</td>';
			echo '<td bgcolor="#' . $alt . '">' . $row->address . '</td>';
			echo '<td bgcolor="#' . $alt . '">' . $row->city . '</td>';
			echo '<td bgcolor="#' . $alt . '">' . $row->state . '</td>';
			echo '<td bgcolor="#' . $alt . '">' . $row->zip . '</td>';
			echo '<td bgcolor="#' . $alt . '">' . format_phone($row->phone_number) . '</td>';
			echo '</tr>';
	}

Link to comment
Share on other sites

You need to increment $row

$row = 0;
      foreach($search_rows as $row) {
         $alt = ($row % 2 == 0)? 'd4e4fe' : 'bfd4f8';
            echo '<tr>';
            echo '<td bgcolor="#' . $alt . '">' . $row->branch_name . '</td>';
            echo '<td bgcolor="#' . $alt . '">' . $row->address . '</td>';
            echo '<td bgcolor="#' . $alt . '">' . $row->city . '</td>';
            echo '<td bgcolor="#' . $alt . '">' . $row->state . '</td>';
            echo '<td bgcolor="#' . $alt . '">' . $row->zip . '</td>';
            echo '<td bgcolor="#' . $alt . '">' . format_phone($row->phone_number) . '</td>';
            echo '</tr>';
            $row++;
      }

Link to comment
Share on other sites

here is my updated code... when i implemented this all of the backgrounds are white and not alternating...

 

$row = 0;
      foreach($search_rows as $row) {
         $alt = ($row % 2 == 0)? 'ff0000' : 'ffffff';
            echo '<tr>';
            echo '<td bgcolor="#' . $alt . '">' . $row->branch_name . '</td>';
            echo '<td bgcolor="#' . $alt . '">' . $row->address . '</td>';
            echo '<td bgcolor="#' . $alt . '">' . $row->city . '</td>';
            echo '<td bgcolor="#' . $alt . '">' . $row->state . '</td>';
            echo '<td bgcolor="#' . $alt . '">' . $row->zip . '</td>';
            echo '<td bgcolor="#' . $alt . '">' . format_phone($row->phone_number) . '</td>';
            echo '</tr>';
            $row++;
      }

Link to comment
Share on other sites

I am stupid. Sorry for setting a bad example.

 

$count = 0;
foreach($search_rows as $row) {
     $alt = (++$count % 2 == 0)? 'd4e4fe' : 'bfd4f8';
     echo '<tr bgcolor="#' . $alt . '">';
     echo '<td>' . $row->branch_name . '</td>';
     echo '<td>' . $row->address . '</td>';
     echo '<td>' . $row->city . '</td>';
     echo '<td>' . $row->state . '</td>';
     echo '<td>' . $row->zip . '</td>';
     echo '<td>' . format_phone($row->phone_number) . '</td>';
     echo '</tr>';
}

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.