Jump to content

pet altenating color code


calmchess

Recommended Posts

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

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";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.