简体   繁体   English

php表中行的颜色

[英]Color of row in php table

I got table that receives data from database.我得到了从数据库接收数据的表。 How can I change color of row when the temperature or humidity is above or under the limits that I get from JSON file.当温度或湿度高于或低于我从 JSON 文件获得的限制时,如何更改行的颜色。 For example: if temperature is higher than minTemp higlight this row green or if temperature is higher than max highlight this row red.例如:如果温度高于 minTemp 突出显示该行绿色或如果温度高于 max 突出显示该行红色。

Here is some code:这是一些代码:

<?php

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->query('SELECT dateandtime, sensor, temperature, humidity FROM temperaturedata ORDER BY dateandtime DESC ');

$tempLimit1 = array();
$fp = fopen('tempLimit.json', 'r');
$bufor = fread($fp, filesize('tempLimit.json'));
fclose($fp);
$tempLimit1 = json_decode($bufor, true);
$minTemp = $templimit1['minTemp'];
$maxTemp = $tempLimit1['maxTemp'];

foreach ($stmt as $row) {
    echo '<tr>';
    echo '<th>' . $row['dateandtime'] . '</th>';
    echo '<th>' . $row['sensor'] . '</th>';
    echo '<th>' . $row['temperature'] . '</th>';
    echo '<th >' . $row['humidity'] . '</th>';
    echo '</tr>';
}
$stmt->closeCursor();

?>

Thank you for all suggestions.谢谢你的所有建议。

You can use a class and format it with css.您可以使用一个类并使用 css 对其进行格式化。

php: php:

echo "<tr class='green'>
  <td>value1</td>
  <td>value2</td>
</tr>;

css: css:

.green td {
  background-color: green;
}

You could do this with inline styling like this:你可以用这样的内联样式来做到这一点:

echo '<tr ';
if($row['temperature']>$minTemp){ echo 'style="background-color:green;"'}
echo '>'

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

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