我可以让div改变它的背景颜色基于它里面的值? [英] Can I make a div change its background color based on the value inside it?
问题描述
我有一个PHP索引页面,其中数据的数据显示在图片上方的< div>
中。我想让< div>
根据内容中的内容更改颜色。
这个:
我有客户 - 日期 - 到期
数组我的意思是有一个如下的结构:
[0] => array('customer'=>'John','date'=>'2012-05-11','due'=> 9),
[ 1] => array('customer'=>'Hess','date'=>'2011-12-11','due'=> 5),
[2] =& array('customer'=>'Mrac','date'=>'2012-06-18','due'=> 3)
)
到期显示的天数 - 我想要< div>
在php代码中,如果Due in小于或等于5,则为div提供一个不同的类。
在你的PHP中,当你遍历数组时:
<?php
echo< div;
if($ myArray [i] ['due']< = 5){echoclass ='dueSoon'; }
echo> $ myArray [i] ['customer'] - $ myArray [i] ['date'] - $ myArray [i] ['due']< / div>
?>
,以便在少于5天时输出以下内容:
< div class =dueSoon> ...< / div>
然后css:
.dueSoon {background-color:red; }
I have a PHP index page where data from an array is being displayed inside <div>
's on top of a picture. I want the <div>
's to change color based on the content inside it.
The idea is this:
I have Customer - Date - Due in
The array I mean has a structure like the following:
Array(
[0] => array('customer' => 'John', 'date' => '2012-05-11', 'due' => 9),
[1] => array('customer' => 'Hess', 'date' => '2011-12-11', 'due' => 5),
[2] => array('customer' => 'Mrac', 'date' => '2012-06-18', 'due' => 3)
)
Due in shows a number of days - I want the <div>
to be red if the number inside it is 5 or lower.
Any help would be greatly appreciated.
Within the php code, give the div a different class if "Due in" is less than or equal to 5. So in your PHP, when you're looping through your array:
<?php
echo "<div";
if($myArray[i]['due'] <= 5){ echo " class='dueSoon'"; }
echo "> $myArray[i]['customer'] - $myArray[i]['date'] - $myArray[i]['due'] </div>";
?>
so that the following gets output when there's less than 5 days:
<div class="dueSoon"> ... </div>
And then css:
.dueSoon { background-color:red; }
这篇关于我可以让div改变它的背景颜色基于它里面的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!