contiw Posted August 7, 2010 Share Posted August 7, 2010 The intention is to alternate table rows background color between classes "row0" and "row1" defined elsewhere. Is this code valid ? <?php $rowclass = 0; ?> <table ... > <!-- BEGIN poll_option --> <tr class="row{ROWCLASS}"> ............ tried also : class="row<?= $rowclass ?>" <td> .......... </td> </tr> <?php $rowclass = 1 - $rowclass; ?> <!-- END poll_option --> </table> In the source I cannot see any value for "$rowclass". Thanks for helping. Quote Link to comment https://forums.phpfreaks.com/topic/210039-alternating-row-colors/ Share on other sites More sharing options...
JasonLewis Posted August 7, 2010 Share Posted August 7, 2010 Check out this: $row = 0; $i = 0; while($i < 30){ $row = $row == 0 ? 1 : 0; echo $row . '<br />'; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/210039-alternating-row-colors/#findComment-1096188 Share on other sites More sharing options...
spfoonnewb Posted August 7, 2010 Share Posted August 7, 2010 <style type="text/css"> .class1 { background: #CC0000; } .class2 { background: #FF0000; } </style> <?php $i = 0; while ($i < 31) : ?> <table> <!-- BEGIN poll_option --> <tr class="<?php if ($i & 1) : echo "class2"; else : echo "class1"; endif; ?>"> <td> .......... </td> </tr> <!-- END poll_option --> </table> <?php $i++; endwhile; ?> Quote Link to comment https://forums.phpfreaks.com/topic/210039-alternating-row-colors/#findComment-1096250 Share on other sites More sharing options...
jcbones Posted August 7, 2010 Share Posted August 7, 2010 Multiple of different ways. <style type="text/css"> .class1 { background: #CC0000; } .class2 { background: #FF0000; } </style> <?php $i = 0; while ($i < 31) : ?> <table> <!-- BEGIN poll_option --> <tr class="<?php echo ((++$i % 2) == 0) ? "class2" : "class1";?>"> <td> .......... </td> </tr> <!-- END poll_option --> </table> Quote Link to comment https://forums.phpfreaks.com/topic/210039-alternating-row-colors/#findComment-1096252 Share on other sites More sharing options...
sinista Posted August 7, 2010 Share Posted August 7, 2010 shouldnt it be with the table tags outside of the loop? or your making lots of tables and not rows <style type="text/css"> .class1 { background: #CC0000; } .class2 { background: #FF0000; } </style> <table> <?php $i = 0; $row = 0; while ($i < 31) { ?> <!-- BEGIN poll_option --> <tr class="<?php $row = $row == 0 ? 1 : 0; ?>"> <td> .......... </td> </tr> <!-- END poll_option --> <?php $i++; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/210039-alternating-row-colors/#findComment-1096253 Share on other sites More sharing options...
spfoonnewb Posted August 7, 2010 Share Posted August 7, 2010 Yeah, that would make the most sense, although I simply used the exact example provided. Yes you can get rid of the extra if logic, however I definitely wouldn't reccomend adding to $i in the IF statement. <style type="text/css"> .class1 { background: #CC0000; } .class2 { background: #FF0000; } </style> <table> <?php $i = 0; while ($i < 31) : ?> <!-- BEGIN poll_option --> <tr class="<?php echo ($i & 1) ? "class2" : "class1"; ?>"> <td> .......... </td> </tr> <!-- END poll_option --> <?php $i++; endwhile; ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/210039-alternating-row-colors/#findComment-1096258 Share on other sites More sharing options...
contiw Posted August 7, 2010 Author Share Posted August 7, 2010 Thanks much everybody. Yes we are talking about a polls digest, a all bunch of tables. Quote Link to comment https://forums.phpfreaks.com/topic/210039-alternating-row-colors/#findComment-1096276 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.