natalieG Posted July 16, 2007 Share Posted July 16, 2007 We want to access mysql table ad have row colors alternate, rows 1,3,5...=white and rows 2,4,6,8..=red. if we have a running row counter[$ROWCOUNT] Then we tried: if ($ROWCOUNT %2 ==0){color=red;}else {color=white:} $ROWCOUNT=$ROWCOUBNT + 1; but all rows are white Thanks, Jennifer Quote Link to comment https://forums.phpfreaks.com/topic/60270-php-arithmetic/ Share on other sites More sharing options...
cmgmyr Posted July 16, 2007 Share Posted July 16, 2007 Here is what I usually do... $rownum = 0; while($row = $db->fetch($result)){ //DB stuff here $tr = $rownum % 2; echo "<tr class=\"tr".$tr."\">"; //out put information echo "</tr>"; $x++; } in the css .tr0{ background-color:red; } .tr1{ background-color:white; } ...something like that, hope it helps Quote Link to comment https://forums.phpfreaks.com/topic/60270-php-arithmetic/#findComment-299779 Share on other sites More sharing options...
sasa Posted July 16, 2007 Share Posted July 16, 2007 try <?php $ROWCOUBNT = 1; for ($i = 1; $i < 10; $i ++){ if ($ROWCOUNT %2 ==0){echo 'red';}else {echo 'white';} $ROWCOUNT=$ROWCOUNT + 1; echo "<br />\n"; } ?> it's work look line $ROWCOUNT=$ROWCOUBNT + 1; Quote Link to comment https://forums.phpfreaks.com/topic/60270-php-arithmetic/#findComment-299789 Share on other sites More sharing options...
roopurt18 Posted July 16, 2007 Share Posted July 16, 2007 Slightly more concise: <?php $n = 0; $colors = Array("red", "white"); for($i = 0; $i < 10; $i++){ echo $colors[$n++ % 2] . "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/60270-php-arithmetic/#findComment-299828 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.