Eiolon Posted July 20, 2011 Share Posted July 20, 2011 This is probably completely wrong but I am trying to figure out how to alternate table row colors. The color I am getting is a bright green for all the rows, even though I clearly specify RED and GRAY. <table> <tr> <th><div align="left">Program</div></th> <th><div align="left">Session</div></th> <th><div align="left">Start Date</div></th> <th><div align="left">End Date</div></th> </tr> <?php do { $color1 = "#FF0000"; $color2 = "#808080"; $count = 0; $color = ($count % 2) ? $color1 : $color2; ?> <tr> <td bgcolor="$row_color"><?php echo $row_programs['pname']; ?></td> <td bgcolor="$row_color"><?php echo $row_programs['sname']; ?></td> <td bgcolor="$row_color"><?php echo $row_programs['start_date']; ?></td> <td bgcolor="$row_color"><?php echo $row_programs['end_date']; ?></td> </tr> <?php $count++; } while ($row_programs = mysql_fetch_assoc($programs)); ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/242403-problem-alternating-table-row-colors/ Share on other sites More sharing options...
jcbones Posted July 20, 2011 Share Posted July 20, 2011 $color1 = "#FF0000"; $color2 = "#808080"; $color = ($color == $color2) ? $color1 : $color2; Quote Link to comment https://forums.phpfreaks.com/topic/242403-problem-alternating-table-row-colors/#findComment-1244979 Share on other sites More sharing options...
Zane Posted July 20, 2011 Share Posted July 20, 2011 I don't see you define $row_color anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/242403-problem-alternating-table-row-colors/#findComment-1244980 Share on other sites More sharing options...
Maknib Posted July 20, 2011 Share Posted July 20, 2011 probably just me but i dont see a $row_color anwhere and it is also not inside PHP it would have to be something like <td bgcolor="<?php $row_color ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/242403-problem-alternating-table-row-colors/#findComment-1244981 Share on other sites More sharing options...
Psycho Posted July 20, 2011 Share Posted July 20, 2011 You are defining $count inside the loop as 0 on each iteration! But, you don't need it anyway. There are other problems in that you are trying to put PHP variables directly in the HTML. Also, the do while loop will fail to have values the first time through. <?php $color1 = "#FF0000"; $color2 = "#808080"; $output = ''; while ($row_programs = mysql_fetch_assoc($programs)) { $row_color = ($row_color==$color1) ? $color2 : $color1; $output .= "<tr>\n"; $output .= "<td bgcolor=\"{$row_color}\">{$row_programs['pname']}</td>\n"; $output .= "<td bgcolor=\"{$row_color}\">{$row_programs['sname']}</td>\n"; $output .= "<td bgcolor=\"{$row_color}\">{$row_programs['start_date']}</td>\n"; $output .= "<td bgcolor=\"{$row_color}\">{$row_programs['end_date']}</td>\n"; $output .= "</tr>\n"; } ?> <table> <tr> <th><div align="left">Program</div></th> <th><div align="left">Session</div></th> <th><div align="left">Start Date</div></th> <th><div align="left">End Date</div></th> </tr> <?php echo $output; ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/242403-problem-alternating-table-row-colors/#findComment-1244985 Share on other sites More sharing options...
Eiolon Posted July 20, 2011 Author Share Posted July 20, 2011 I see. I was trying to separate the PHP from the HTML to make it easier for me to work with. The above code works, but it also outputs an error that $row_color is an undefined variable. Quote Link to comment https://forums.phpfreaks.com/topic/242403-problem-alternating-table-row-colors/#findComment-1245163 Share on other sites More sharing options...
Psycho Posted July 20, 2011 Share Posted July 20, 2011 The above code works, but it also outputs an error that $row_color is an undefined variable. Yeah, that is a warning because the first time you check "($row_color==$color1)" it is not yet defined. But, you shouldn't be displaying warning errors in your production environment anyway. So, you can either ignore it or just define $row_color as $color1 before the loop starts $color1 = "#FF0000"; $color2 = "#808080"; $row_color = $color1; // <=== NEW LINE $output = ''; while ($row_programs = mysql_fetch_assoc($programs)) { Quote Link to comment https://forums.phpfreaks.com/topic/242403-problem-alternating-table-row-colors/#findComment-1245200 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.