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? 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 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 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
Archived
This topic is now archived and is closed to further replies.