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

问题描述

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

如果温度高于13,数字将以红色显示

如果温度在0到12之间,则数字应显示为白色

如果温度低于0,则数字应显示为蓝色

我目前已经尝试过:

 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行代码。当我要显示7天和3个位置时,它的9x7 * 3 = 189行+间距,您就明白了。

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

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

解决方法

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

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; ?>
,

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

,

使用数组设置颜色white的上下限,并使用foreach()构造正确的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) {
      ...
    }