calmchess Posted December 17, 2009 Share Posted December 17, 2009 I ran across this code on this forum for alternating the color of rows or in my case div backgrounds anyway I want to make small blocks of diffrent colors and alterante between them so i'm wondering how i could modify this script for say 10 colors. //css $i =0; for($j =0;$j<10;$j++){ $class = ($i++ % 2) ? 'odd' : 'even'; echo "<div class=$class>test</div>"; } Link to comment https://forums.phpfreaks.com/topic/185531-pet-altenating-color-code/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2009 Share Posted December 17, 2009 I would make an array of the class names, then just assign them in sequence to each <div > - <?php $class_names = array(); $class_names[] = 'color0'; // some unique class name for the first color ... $class_names[] = 'color1'; $class_names[] = 'color2'; $class_names[] = 'color3'; $class_names[] = 'color4'; $class_names[] = 'color5'; $class_names[] = 'color6'; $class_names[] = 'color7'; $class_names[] = 'color8'; $class_names[] = 'color9'; $i =0; for($j =0;$j<20;$j++){ $class = $i++ % 10; echo "<div class='$class_names[$class]'>test</div>\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/185531-pet-altenating-color-code/#findComment-979535 Share on other sites More sharing options...
calmchess Posted December 17, 2009 Author Share Posted December 17, 2009 thanks worked like a charm Link to comment https://forums.phpfreaks.com/topic/185531-pet-altenating-color-code/#findComment-979544 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.