-1

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

  • `=` is an assignment operator. Can you assign something to a `return`? – El_Vanja Feb 01 '21 at 00:32
  • 1
    Does this answer your question? [PHP parse/syntax errors; and how to solve them](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – El_Vanja Feb 01 '21 at 00:32

1 Answers1

1

As per the comments:

return = '#FF8000';

Should be:

return '#FF8000';

You can also achieve this in the query directly with CASE

SELECT *, 
CASE 
    WHEN temperature < 15 THEN '#FF8000'
    WHEN temperature BETWEEN 15 AND 25 THEN '#00FF00'
    WHEN temperature > 25 THEN '#FF0000'
    ELSE '#CCC'
END AS temperature_color,
CASE 
    WHEN humidity < 20 THEN '#FF0000'
    WHEN humidity BETWEEN 20 AND 50 THEN '#00FFFF'
    WHEN humidity BETWEEN 50 AND 99 THEN '#0000FF'
    WHEN humidity > 99 THEN '#FF0000'
    ELSE '#CCC'
END AS humidity_color
                 FROM `weather` 
                 ORDER BY id DESC
                 limit 1
Blueline
  • 388
  • 1
  • 10
  • I like the idea using CASE looks a lot easier. With your code Im currently having an error : Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in \include\wth_inc.php on line 40 and line 40 is : $num = mysqli_num_rows($result); – Inna Ivanova Feb 01 '21 at 00:54
  • 1
    Can you post the query, seems like it's got an error. Also, I just saw I had an extra comma before FROM delete that and it'll probably work. – Blueline Feb 01 '21 at 10:44