如何根据温度显示不同的颜色?

How to display different color depending on temperature?

我正在从 XML 接收数据,其中包含有关某些位置的天气信息。我正在获取有关温度的信息,我想这样显示:

If the temperature is above 13, the numbers shall be displayed in red

If the temperature is between or equal to 0 and 12, the numbers shall be displayed in white

If the temperature is lower than 0, the numbers shall be displayed in blue

我目前试过这个:

 if ($result[0]['temperature'] > 13){
    $temperature = "<span style='color:#FD1B1C;'>". $result[0]['temperature']. "°"."</span>";
  }
  if ($result[0]['temperature'] >=0 && $result[0]['temperature'] <=12){
    $temperature = "<span style='color:white;'>". $result[0]['temperature']. "°"."</span>";
  }
  if ($result[0]['temperature'] < 0){
    $temperature = "<span style='color:blue;'>". $result[0]['temperature']. "°"."</span>";
  }

此代码^ 有效,但很快就会变得很长且“不清楚”。这 $temperature 只有一个温度,当我想显示 7 个温度(一周中的每一天一个)和多个位置时,我很快就会得到很多“$temperature n”变量和行。示例:我展示的一个温度是 9 行代码。当我想在 3 个位置显示 7 天时,它是 9x7*3 = 189 行 + 间距,你明白了。

也许最好的办法是将此代码放在另一个文件中并直接引用它?

相关信息: XML 显示每 6 小时(0-6、6-12、6-18、18-24)的温度,这意味着我每天获取 $result[0],[4],[8],[12]

您可以尝试创建一个数组,每个位置都包含一个包含所有温度的数组。当您遍历数组时,您可以使用 switch 语句来检查每个温度结果,这样您就没有 100 个 if 语句。

使用数组设置颜色的下限和上限whiteforeach()构造正确的HTML。这样您就可以容纳任意数量的数组条目(温度)。

<?php
$limits = [0, 12]; // below 0 = blue, between 0 and 12 = white, above 12 = red
$result = [
    ['temperature' => -3,], ['temperature' => 9,], ['temperature' => 13,], ['temperature' => 6,],
    ['temperature' => -5,], ['temperature' => 11,], ['temperature' => 17,], ['temperature' => 4,],
];

$tempHtml = []; // store for HTML
foreach ($result as $key => $value) {
    $temp = $value['temperature'];
    $color = 'white'; // default
    if ($temp > $limits[1]) {
        $color = '#FD1B1C'; // red
    } elseif ($temp < $limits[0]) {
        $color = 'blue';
    }
    $tempHtml[] = "<span style='color:{$color}'>" . $value['temperature'] . "°" . "</span>";
}

echo '<pre>';
var_dump($tempHtml);
echo '</pre>';

输出:

array (size=8)
  0 => string '<span style='color:blue'>-3°</span>' (length=36)
  1 => string '<span style='color:white'>9°</span>' (length=36)
  2 => string '<span style='color:#FD1B1C'>13°</span>' (length=39)
  3 => string '<span style='color:white'>6°</span>' (length=36)
  4 => string '<span style='color:blue'>-5°</span>' (length=36)
  5 => string '<span style='color:white'>11°</span>' (length=37)
  6 => string '<span style='color:#FD1B1C'>17°</span>' (length=39)
  7 => string '<span style='color:white'>4°</span>' (length=36)

注意: 如果您需要定位元素 [0]、[4]、[8]... 将 foreach 中的逻辑包装在 modulo 条件:

foreach ($result as $key => $value) {
    if($key %4 === 0) {
      ...
    }

制作一个断点数组,并根据断点分配颜色:

function getColor($temp) {
  $defaultColor = 'blue';
  $breakPoints = [
    '0' => 'white',
    '13' => 'red',
    ];
  
  $out = $defaultColor;
  foreach($breakPoints as $target => $color) {
      if ( (float)$target <= (float)$temp) {
          $out = $color;
      }
  }
  return $out;
}

由于我们按升序设置断点,因此我们将默认颜色设置为负值的颜色。然后,当我们遍历断点时,不断改变颜色,直到断点大于被测温度。

请注意,将其放入函数中可以在整个脚本中使用它:

<?php
// do all your php stuff, including the getColor function
?>
<!-- html stuff -->

<?php foreach($result as $row): ?>
  <!-- display the row in html -->

  <div>The temperature is 
    <span style="color:<?= getColor($row['temperature']) ?>">
      <?= $row['temperature'] ?> °
    </span>
  </div>

<?php endforeach; ?>