dennismonsewicz Posted May 18, 2009 Share Posted May 18, 2009 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>'; } } Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 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>'; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 18, 2009 Author Share Posted May 18, 2009 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>'; } Quote Link to comment Share on other sites More sharing options...
Brian W Posted May 18, 2009 Share Posted May 18, 2009 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++; } Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 Sorry, I noticed that and edited the code. Apparently you got in too soon. See my above code again. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 18, 2009 Author Share Posted May 18, 2009 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++; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 18, 2009 Author Share Posted May 18, 2009 Bump Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 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>'; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted May 18, 2009 Author Share Posted May 18, 2009 Awesome! This works! TKS! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.