etrader Posted June 21, 2011 Share Posted June 21, 2011 I have a for loop as for ($i=0; $i < $pageLength; $i++) { ... echo "<tr class='odd'><td>...</td>"; How I can set class "odd" and "even" for the results? Quote Link to comment https://forums.phpfreaks.com/topic/239977-odd-and-even-in-a-for-loop/ Share on other sites More sharing options...
kenrbnsn Posted June 21, 2011 Share Posted June 21, 2011 Change <?php echo "<tr class='odd'><td>...</td>"; ?> to <?php $class = ($class == 'odd')?'even':'odd'; echo "<tr class='$class'><td>...</td>"; ?> and put <?php $class = 'even'; ?> before the loop Or change <?php echo "<tr class='odd'><td>...</td>"; ?> to <?php $class = ($i%2 != 0)?'odd':'even'; echo "<tr class='$class'><td>...</td>"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/239977-odd-and-even-in-a-for-loop/#findComment-1232699 Share on other sites More sharing options...
etrader Posted June 21, 2011 Author Share Posted June 21, 2011 Thanks Ken! Works like a charm Quote Link to comment https://forums.phpfreaks.com/topic/239977-odd-and-even-in-a-for-loop/#findComment-1232705 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.