【问题标题】:How to display different color depending on temperature?如何根据温度显示不同的颜色?
【发布时间】:2020-11-07 10:11:46
【问题描述】:

我正在接收来自 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] 以获取每一天的温度。

【问题讨论】:

  • 如果你的温度正好是 13 怎么办?你可以......在你的ifs 中只设置颜色,你所有的ifs 使用类似$temperature = "&lt;span style='color:$color'&gt;". $result[0]['temperature']. "°"."&lt;/span&gt;";

标签: php xml temperature


【解决方案1】:

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

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

【讨论】:

  • 这会打印整个温度数组。我该如何选择我想要的?使用$result[0]['temperature'],当我想要数组中的第四个时,我可以将[0] 更改为例如[4]`,但$row[0]['temperature'] 不起作用。
  • 第二个示例展示了一种遍历$result 数组并显示所有结果的方法,假设$result 的每一行本身就是一个带有名为temperature 的键的数组。请记住,在通过$result 的第一次迭代中,$row$result[0] 相同。然后在第二次迭代中,$row$result[1] 相同。因此,如果您只想要索引 4,则不会遍历结果数组,只需对其进行硬编码:getColor( $result[4]['temperature'] ) 但如果您只想显示一个结果,为什么将其放入结果数组中?
  • 我现在可以使用它了,谢谢。它在数组中的原因是由于我是如何从 XML 中导入它的:$feed = simplexml_load_file($url) or die('Can not connect to server'); $result = array(); foreach ($feed-&gt;forecast-&gt;tabular-&gt;time as $content) {array_push($result, [ "from" =&gt; (string)$content['from'], "to" =&gt; (string)$content['to'], 'symbol' =&gt; (string)$content-&gt;symbol['name'], 'temperature' =&gt; (string)$content-&gt;temperature['value'], ]);} 我可能可以用另一种方式来做,但我就是这样做的。我不是专业人士...
  • 啊,XML。糟糕... ;)
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    使用数组设置颜色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) {
          ...
        }
    

    【讨论】:

      猜你喜欢
      • 2011-11-05
      • 2013-04-30
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 2014-05-11
      相关资源
      最近更新 更多