Sethm Posted December 17, 2009 Share Posted December 17, 2009 I want to set up my rows to display so based on their id's they can have a light grey background or white - just for organization purposes. Does anyone have an idea how I would go about this using PHP scripting? Thanks in advance! Seth Quote Link to comment https://forums.phpfreaks.com/topic/185526-alternating-row-color/ Share on other sites More sharing options...
premiso Posted December 17, 2009 Share Posted December 17, 2009 You want to use the modulus ( % ) operator. $i=0; $x = 10; while ($i < $x) { if (($i % 2) == 0) echo "White <br />"; else echo "Grey <br />"; $i++; } Is a rough example of how you would use it. Quote Link to comment https://forums.phpfreaks.com/topic/185526-alternating-row-color/#findComment-979489 Share on other sites More sharing options...
Sethm Posted December 17, 2009 Author Share Posted December 17, 2009 Sweet! Thank you! I'll mark it solved when I finally figure out if it works! Quote Link to comment https://forums.phpfreaks.com/topic/185526-alternating-row-color/#findComment-979491 Share on other sites More sharing options...
JAY6390 Posted December 17, 2009 Share Posted December 17, 2009 <tr class="<?php echo ($i++ % 2) ? 'odd' : 'even'; ?>"> Use this for your row. It will alternate between the classes odd and even, so just style those two classes in your css and you should be fine Quote Link to comment https://forums.phpfreaks.com/topic/185526-alternating-row-color/#findComment-979494 Share on other sites More sharing options...
Sethm Posted December 17, 2009 Author Share Posted December 17, 2009 Jay, The method you gave me worked quite smoothly. I was a bit confused with the other one. I was trying to wrap my head around it and would have loved to understand it, but the way you presented it worked without me thinking so thank you for helping me. When I get more time and don't have to slam things together I will have to learn what the modulus operator is and how it really functions - so thank you Premiso for pointing me in the right direction! Seth Quote Link to comment https://forums.phpfreaks.com/topic/185526-alternating-row-color/#findComment-979506 Share on other sites More sharing options...
JAY6390 Posted December 17, 2009 Share Posted December 17, 2009 Well not to throw you in the deep end but the real magic is the ternary operator in that code Quote Link to comment https://forums.phpfreaks.com/topic/185526-alternating-row-color/#findComment-979508 Share on other sites More sharing options...
Sethm Posted December 17, 2009 Author Share Posted December 17, 2009 Is that the two ++ signs? ...guess I could search it out. Thanks - I'm slowly learning this PHP coding thing. It's a process - but I have to admit ..it is fun! Quote Link to comment https://forums.phpfreaks.com/topic/185526-alternating-row-color/#findComment-979510 Share on other sites More sharing options...
JAY6390 Posted December 17, 2009 Share Posted December 17, 2009 No it's the ? and the : that make a small shorthand "if" statement. It's the same as having if($i++ % 2) { echo 'odd'; }else{ echo 'even'; } Quote Link to comment https://forums.phpfreaks.com/topic/185526-alternating-row-color/#findComment-979512 Share on other sites More sharing options...
Sethm Posted December 17, 2009 Author Share Posted December 17, 2009 That does sound extremely useful and simple. Thanks for the short lesson! You're a good sport! Quote Link to comment https://forums.phpfreaks.com/topic/185526-alternating-row-color/#findComment-979518 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.