根据时间值php改变文本的颜色

时间:2018-02-19 10:50:17

标签: javascript php html css function

我想尝试使用我的时间文本获取交通灯颜色方案,如果时间在0.0000和10.0000之间,文本如果是绿色,如果在10.0100和15.0000之间,文本是橙色,在15.0100和20.0000之间那么它的红色我不能得到我运行的功能可能会遗漏一些但我不确定是什么。

当前mysql查询返回结果为14.6263,此值不断变化

我目前的代码是:

<!doctype html>
<html>
<head>

<title>time stats</title>

<style>
body {
background-color: black;
}
.box1 {
width: 300px;
height: 400px;
border: 15px solid yellow;
padding: 15px;
margin: 15px;
display: inline-block;
color: white;
font-size:40px;
color: lime;

.container  {
text-align: center;
}
</style>
<body>
<div class="box1">
<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','username','password','test');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="select avg(time_format(delivery_avg,'%i:%s')) as time_avg from test.del where location = 'yellow'";
$result = mysqli_query($con,$sql);

echo "<table>
<thead>
<th>time Average</th>
</thead>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['time_avg'] . "</td>";
echo "</tr>";}
echo "</table>";
mysqli_close($con);
?>
</div>
  function updatecolor() {
        //console.log('function called');
        {
if ($box1.td < 10)
    return '#00FF00';
else if ($box1.td >= 11 && $box1.td<= 15)
    return = '#FFA500';
else if ($box1.td >= 16 && $box1.td<= 20)
    return = '#ff0000';
}
        });

    }
    var updateInterval = setInterval(updatecolor, 1000);
</body>
</html>

1 个答案:

答案 0 :(得分:1)

这是一个根据函数的$valueToCheck参数返回颜色的小函数:

<?php
function addColor($valueToCheck) {
    {
        if ($valueToCheck <= 10) {
            return '#00FF00';
        } else if ($valueToCheck >= 11 && $valueToCheck <= 15) {
            return '#FFA500';
        } else if ($valueToCheck >= 16 && $valueToCheck <= 20) {
            return '#ff0000';
        }
    };
}
?>

将其包含在<?php ?>标签中,因为它是一个PHP函数。

在HTML中使用它,如下所示:

while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td style=\"color:" . addColor($row['time_avg']) . "\">";
  echo $row['time_avg'];
  echo "</td>";
  echo "</tr>";
}

至于你的代码:

return = '#FFA500';无效,请移除=

如果$box1可用:

if ($box1.td < 10)
    return '#00FF00';
else if ($box1.td >= 11 && $box1.td<= 15)

10怎么样?您永远不会检查10>x>11