Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.