简体   繁体   English

根据时间值更改文本颜色php

[英]changing colour of text dependent on time value php

I want to try get a traffic light colour scheme with my time text where if the time is between 0.0000 and 10.0000 the text if green if between 10.0100 and 15.0000 the text is orange and between 15.0100 and 20.0000 then its red i cant get my function to run i may be missing something but im not sure what.我想尝试使用我的时间文本获得交通灯配色方案,如果时间在 0.0000 和 10.0000 之间,如果文本为绿色,如果在 10.0100 和 15.0000 之间,文本为橙色,在 15.0100 和 20.0000 之间,则它的红色我无法得到我的功能运行我可能会遗漏一些东西,但我不确定是什么。

currently the mysql query returns result as 14.6263 with this value constantly changing当前 mysql 查询返回结果为 14.6263,此值不断变化

my current code is :我目前的代码是:

<!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>

Here's a little function that will return your colors depending on the $valueToCheck parameter of the function:这是一个小函数,它将根据函数的$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';
        }
    };
}
?>

Wrapped it in <?php ?> tags, since it's a PHP function.将它包裹在<?php ?>标签中,因为它是一个 PHP 函数。

Use it in your HTML like so:在您的 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>";
}

As for your code:至于你的代码:

return = '#FFA500'; is not valid, remove the = .无效,删除=

If $box1 were available:如果$box1可用:

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

What about 10 ? 10呢? You never check for 10>x>11 .你从不检查10>x>11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM