[英]How to give 3 different color for div's in loop in php
Trying to give 3 different color for div's in loop in php, now its working only two .how can i impliment alternative color for div.? 试图为php中的div循环提供3种不同的颜色,现在它只有两种。我可以为div实现替代颜色。 Here what I want.
这就是我想要的。
style 样式
.redBackground { background-color:#F00; }
.blueBackground { background-color:#03F;}
.greenBackground { background-color:#6F3; }
php 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 } ?>
Use color array like below 使用如下颜色数组
$color_array = array('whiteBackground', 'grayBackground', 'blueBackground');
$x=0;
while($row=mysql_fetch_array($new)){
$x++;
$class = $color_array[$x%3];
}
And in future, if you want to more color, then simply add color-class in array and change in $color_arrar[$x%n]
, where n=number_of_color 在将来,如果你想要更多颜色,那么只需在数组中添加颜色类并更改
$color_arrar[$x%n]
,其中n = number_of_color
If you want random color, then use below code 如果您想要随机颜色,请使用下面的代码
$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];
}
Please try this 请试试这个
if($x%3 == 0)
$class = 'greenBackground';
else if($x%2 == 0 )
$class = 'redBackground';
else
$class = 'blueBackground';
If you need it in random order you can use array_rand
function 如果您需要随机顺序,可以使用
array_rand
函数
$color = array("redBackground", "blueBackground", "greenBackground");
$colorValue = array_rand($color, 1);
$class = $color [$colorValue];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.