Home Weather Station, I want to change the text colour based on value. Link to another similar question: PHP: Change color of text based on $value Currently, python script takes the data from the DHT22 sensor and saves it into MySQL database.Then simply fetch the last id into PHP.
My goal is to change each result with a separate set of colours e.g.
Temperature - 0-15 - blue
15.1 - 25 - green
25.1 - 30 - red
Humidity - 0%-20% - red
20.1-50 - yellow
50.1-99 - blue
99-100 - red
Code that I try to understand and implement into my existing code:
function getProperColor($number)
{
if ($var > 0 && $var <= 5)
return '#00FF00';
else if ($var >= 6 && $var <= 10)
return = '#FF8000';
else if ($var >= 11)
return = '#FF0000';
<div style="background-color: <?=getProperColor($result['number'])?>;"><?=$result["title"]?></div>
This code was provided by @sshow
Unfortunately, I did not succeed with clean code
My Code
<!-- fetchqry taking the result from weather table-->
<?php
$fetchqry = "SELECT *
FROM `weather`
order by id
desc
limit 1;";
$result= mysqli_query($conn,$fetchqry);
$num = mysqli_num_rows($result);
if($num > 0){
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$conn->close();
?>
<!-- Printing Temperature & Humidity-->
<div><h5><i class="fas fa-temperature-high"></i> Temperature : <font size=6><?php echo $row['temperature']; ?> C°</font></h5></div>
<div><h5><i class="fas fa-tint"></i> Humidity : <font size=6><?php echo $row['humidity']; ?> %</font></h5></div>
Messed up code:
<!-- fetchqry1 taking the result from weather table-->
<?php
$fetchqry = "SELECT *
FROM `weather`
order by id
desc
limit 1;";
$result= mysqli_query($conn,$fetchqry);
$num = mysqli_num_rows($result);
if($num > 0){
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
function getProperColor($number)
{
if ($var > 0 && $var <= 5)
return '#00FF00';
else if ($var >= 6 && $var <= 10)
return = '#FF8000';
else if ($var >= 11)
return = '#FF0000';
}
$conn->close();
?>
<!-- Printing Temperature & Humidity-->
<div style="background-color: <?=getProperColor($result['number'])?>;"><?=$result["title"]?><h5><i class="fas fa-temperature-high"></i> Temperature : <font size=6><?php echo $row['temperature']; ?> C°</font></h5></div>
This gives me an error:
Parse error: syntax error, unexpected '=', expecting ';' in \include\wth_inc.php on line 36
Line 36 is:
return = '#FF8000';
Thanks