2
Well I want to create a sequence of DIV
, only that I want each DIV
have in your background-color
a previously settled random color, type black, yellow and green, but I do not want these colors to repeat one after the other.
Type :yellow >> yellow >> black>> green >> black
I want it to look something like this :: yellow >> black >> yellow >> green >> black
Currently I use this code to generate random colors pro background-color
, but this code repeats the colors one after the other, since the random color is generated in the refresh of the page.
<?php
$cor = array();
$cor[1] = "#CFF";
$cor[2] = "#9FF";
$cor[3] = "#600";
$cor[4] = "#FF0";
$cor[5] = "#C69";
$cor[6] = "#0F0";
$contador = count($cor);
$aleatorio = rand(1,$contador);
?>
<style>
div{margin-left:15px; width:100px; height:100px; float:left}
</style>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
<div style="background-color:<?php echo $cor[$aleatorio] ?>"></div>
In this case my problem is not to repeat the colors, but to repeat the same color one after the other. That is why I quoted the above pattern " yellow >> yellow >> black >> green >> black", in which case equal colors repeat one after the other. The colors can even repeat COUNTLESS times, but I don’t want it to repeat one after the other, I want it to be something like :: Yellow >> black >> Yellow >> black
I didn’t understand the problem, is to repeat the colors in the same load (each div should be with a different color) or in different loads (all Ivs of the same color, but changing with each refresh)?
– luigibertaco
@luigibertaco the problem is not to repeat the colors, but to repeat the same color one after the other. So I quoted that pattern in the example " yellow >> yellow >> black >> green >> black". In case the colors can repeat NUMEROUS times, but I don’t want it to repeat after each other, type :: Yellow >> yellow >> black >> black
– ivan veloso
then the @Bacco answer solves your problem.
– luigibertaco
@ivanveloso if you are curious to see, I gave a beautiful optimized code. The logic is the same, but I left cleaner and objective. Jai delete this comment here.
– Bacco