简体   繁体   中英

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.

currently the mysql query returns result as 14.6263 with this value constantly changing

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:

<?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.

Use it in your HTML like so:

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:

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

What about 10 ? You never check for 10>x>11 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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