[英]How to give 3 different color for div's in loop in php
試圖為php中的div循環提供3種不同的顏色,現在它只有兩種。我可以為div實現替代顏色。 這就是我想要的。
樣式
.redBackground { background-color:#F00; }
.blueBackground { background-color:#03F;}
.greenBackground { background-color:#6F3; }
PHP
<?php
$new= mysql_query("select * from tbl_news");
while ($row=mysql_fetch_array($new))
{
$x++;
$class = ($x%2 == 0)? 'redBackground': 'blueBackground' ;
echo "<tr class='$class'>";
$id = $row['id'];
$news = $row['news'];
?>
<div class="col-md-2 col-sm-12 col-xs-12 news_main_div">
<div class="md-trigger" data-modal="modal-11">
<div <?php echo "<tr class='$class'> ";?>>
<h1 style=" margin-bottom:5px;">
<strong ><?php echo $news ?></strong>
</h1></div></div><?php } ?>
使用如下顏色數組
$color_array = array('whiteBackground', 'grayBackground', 'blueBackground');
$x=0;
while($row=mysql_fetch_array($new)){
$x++;
$class = $color_array[$x%3];
}
在將來,如果你想要更多顏色,那么只需在數組中添加顏色類並更改$color_arrar[$x%n]
,其中n = number_of_color
如果您想要隨機顏色,請使用下面的代碼
$color_arrar = array('whiteBackground ','grayBackground ','blueBackground ');
$size_of_array = sizeof($color_arrar);
while($row=mysql_fetch_array($new)){
$n = rand(0,$size_of_array-1);
$class = $color_arrar[$n%3];
}
請試試這個
if($x%3 == 0)
$class = 'greenBackground';
else if($x%2 == 0 )
$class = 'redBackground';
else
$class = 'blueBackground';
如果您需要隨機順序,可以使用array_rand
函數
$color = array("redBackground", "blueBackground", "greenBackground");
$colorValue = array_rand($color, 1);
$class = $color [$colorValue];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.