ted_chou12 Posted December 8, 2006 Share Posted December 8, 2006 [color=red]Use modulus (%)<table>[code]<?$i = 1; $max = 50;while ($i < $max){if ($i%2 == 0){?><tr bgcolor="#aaaaaa"><td></td></tr><?}//end ifelse {?><tr bgcolor="#ffffff"><td></td></tr><?}//end else$i++;}//end while</table>[/code]What modulos does is divides the number you have by the modulos: example $i = 4 (making it 4/2) and takes the remainder (ie. 0). If the modulos (in this case 0) is equal to what we are looking for,background color #aaaaaa, else #ffffffHope that made sense[/color]this code is retained from some other sites, i just have a question to ask:do you always have to set max & min of it? because if they are not set the table seems to loop non-stop, but if i keep the max constant, my data keeps changing, wouldn't the last rows look repeated? ??? Link to comment https://forums.phpfreaks.com/topic/29953-alternative-row-coloring-for-php-pages-tables/ Share on other sites More sharing options...
.josh Posted December 8, 2006 Share Posted December 8, 2006 that $max has nothing to do with alternating row colors. the modulus condition is what alternates it. the $max looks like it is just setting how many rows to actually display. If you do not set them, you create in infinite loop, because it loops WHILE $i is less than $max. If you do not set them, the condition will never be false, hence the infinite loop. Link to comment https://forums.phpfreaks.com/topic/29953-alternative-row-coloring-for-php-pages-tables/#findComment-137615 Share on other sites More sharing options...
eric1235711 Posted December 8, 2006 Share Posted December 8, 2006 make a class to use like this:[code]<?php$a = new alternator;echo "<table>";while(/*condition*/){ $color = $a->go();echo "<tr bgcolor='$color'> ";echo "<td>Lorem Ipsum sit amet</td>"echo "</tr>";}echo "</table>";?>[/code] Link to comment https://forums.phpfreaks.com/topic/29953-alternative-row-coloring-for-php-pages-tables/#findComment-137628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.