forumnz Posted January 25, 2008 Share Posted January 25, 2008 What should I look up on php.net regarding swapping bg colors in multiple row displays. It has a % in it i think. I want row 1 to have bg color $a and row 2 to have $b, 3 to have $a, 4 to have $b etc. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/87836-that-thingy/ Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 <?php $color1 = "#FFFFFF"; $color2 = "#000000"; $i=0; while($i<10) { if($i % 2 == 0) { $bgColor = $color1; } else { $bgColor = $color2; } $i++; } ?> Obivously replace your while() loop etc, it was just to show how it works. Aslong as you have something in your loop so you can keep track of the total number of loops, otherwise it won't alternate. Quote Link to comment https://forums.phpfreaks.com/topic/87836-that-thingy/#findComment-449314 Share on other sites More sharing options...
forumnz Posted January 25, 2008 Author Share Posted January 25, 2008 Thanks but what is the function? I want to look it up on php.net. Thanks, Sam. Quote Link to comment https://forums.phpfreaks.com/topic/87836-that-thingy/#findComment-449317 Share on other sites More sharing options...
KrisNz Posted January 25, 2008 Share Posted January 25, 2008 modulo - thats the remainder of a division of two numbers. Quote Link to comment https://forums.phpfreaks.com/topic/87836-that-thingy/#findComment-449319 Share on other sites More sharing options...
roopurt18 Posted January 25, 2008 Share Posted January 25, 2008 % is the modulo operator. It returns the remainder of a division. Thus you can alternate between indexes into an array with the following: $arr = Array('white', 'black'); for($i = 0; $i < 10; $i++){ echo $arr[$i % 2]; } Quote Link to comment https://forums.phpfreaks.com/topic/87836-that-thingy/#findComment-449324 Share on other sites More sharing options...
forumnz Posted January 25, 2008 Author Share Posted January 25, 2008 Thanks, I have this loop: while($row = mysql_fetch_array($result)) { $business_name = $row['business_name']; echo "<div id=listingmain>" . $business_name . "</div>"; } How would I incorporate the 'swapping' into it? I think I would have to do something in the first div? Like: echo "<div id=listingmain style=background-color:#" . $arr . ";>" . $business_name . "</div>"; Is that along the right lines? Thanks, Sam. Quote Link to comment https://forums.phpfreaks.com/topic/87836-that-thingy/#findComment-449326 Share on other sites More sharing options...
roopurt18 Posted January 25, 2008 Share Posted January 25, 2008 You need: 1) A variable that counts 2) An array that contains the values to toggle between. I don't see either. Also, not placing your HTML-attribute values inside of quotes is wrong, wrong, wrong! Quote Link to comment https://forums.phpfreaks.com/topic/87836-that-thingy/#findComment-449328 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.