【问题标题】:Adding a line of text with a colored cell based the latest observations根据最新观察添加一行带有彩色单元格的文本
【发布时间】:2018-04-13 01:02:44
【问题描述】:

需要帮助获取以下代码并在 MSG_TXT foreach 部分之后添加一行,内容如下:

Recreational Activity - Safe or Caution or Dangerous

取决于观察到的排放数据中的最新排放水平。

  • 45-499 Safe in Green 一个带有白色字体的绿色块
  • 500-799 白色字体黄色块注意
  • 以及任何超过 800 Dangerous 的亮红色白色字体。

这是我当前的代码:

<?php
$url = "http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml";
$xml = simplexml_load_file($url);
?>

<?php foreach ($xml->RESULTSET[0]->ROW as $MSG) :?>
<?php echo '<h4>', $MSG->MSG_TXT; '</h4>'; ?>
<?php endforeach; ?>
&nbsp;

<!-- Table Style -->
<style>
table {border: 2px solid #fff;}
table td {height: 15px;}
table td {border: 1px solid #fff; }
table tr {border: 1px solid #fff; }
table td {padding: 3px; }
</style>

<h2>Observed Data</h2>
<table>
<div style="overflow-x:auto;">
<thead>
<tr>
<td><span style="margin:0px; font-weight:bold">Day</span></td>
<td><span style="margin:0px; font-weight:bold">Time(EST)</span></td>
<td><span style="margin:0px; font-weight:bold">Reservoir Elev.(behind    dam)*</span</td>
<td><span style="margin:0px; font-weight:bold">Tailwater Elev.(below dam)*</span></td>
<td><span style="margin:0px; font-weight:bold">Avg Hourly Discharge*  </span></td>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET[1]->ROW as $obs) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h2>Predicted Data</h2>
<table>
<div style="overflow-x:auto;">
<thead>
<tr>
<td><span style="margin:25px; font-weight:bold">Day</span></td>
<td><span style="margin:5px; font-weight:bold">Average Inflow* </span</td>
<td><span style="margin:5px; font-weight:bold">Midnight Elevation*</span></td>
<td><span style="margin:5px; font-weight:bold">Average Outflow*</span></td>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET[2]->ROW as $pred) :?>
<tr>
<td><?php echo $pred->PREDICTED_DAY; ?></td>
<td><?php echo $pred->DAILY_AVG_INFLOW; ?></td>
<td><?php echo $pred->MIDNIGHT_ELEV; ?></td>
<td><?php echo $pred->DAILY_AVG_OUTFLOW; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>`

更新:

感谢下面的 cmets,我能够将其添加到代码中并获得除了安全、谨慎或危险的输出之外的所有内容。它正在输出流量数。我该如何切换?

 <?php
$discharge = (int) $xml->RESULTSET[1]->ROW[46]->AVG_HOURLY_DISCHARGE;
$discharge_range = [
    'safe'      => [ 'min_range' => 0, 'max_range' => 499 ], 
    'caution'   => [ 'min_range' => 500, 'max_range' => 799 ], 
    'dangerous' => [ 'max_range' => 800 ]
];
$discharge_class = function ($value) use ($discharge_range) {
    foreach ($discharge_range as $key => $range) {
        if (filter_var($value, FILTER_VALIDATE_INT, ['options' => $range]) !== false) {
            return $key;
        }
    }
};
echo "Recreational Activity - ";
echo '<span class="discharge-'.$discharge_class($discharge).'">'.$discharge.'</span>';
?>

【问题讨论】:

  • 在下面查看我的编辑..

标签: php xml html xml-parsing


【解决方案1】:

这是你可以做到的一种方法,有很多方法可以检查一个值是否在一个范围内。

定义你的 CSS

<style>
    .discharge-safe {
        background: green;
        color: white;
    }

    .discharge-caution {
        background: yellow;
        color: white;
    }

    .discharge-dangerous {
        background: red;
        color: white;
    }
</style>

从您的 XML 中获取您要检查的值,然后使用 filter_var 和一个最小值和一个最大值来检查值并分配一个用于选择 CSS 类的 var。

<?php
$discharge = (int) $xml->RESULTSET[1]->ROW[0]->AVG_HOURLY_DISCHARGE;

$discharge_range = [
    'safe'      => [ 'min_range' => 0, 'max_range' => 499 ], 
    'caution'   => [ 'min_range' => 500, 'max_range' => 799 ], 
    'dangerous' => [ 'max_range' => 800 ]
];

$discharge_class = function ($value) use ($discharge_range) {
    foreach ($discharge_range as $key => $range) {
        if (filter_var($value, FILTER_VALIDATE_INT, ['options' => $range]) !== false) {
            return $key;
        }
    }
};

echo '<span class="discharge-'.$discharge_class($discharge).'">'.$discharge.'</span>';
?>

https://3v4l.org/0RRXY

您也可以只使用一个沼泽标准 if else 来比较值,但其中的乐趣在于:

if ($discharge > 0 && $discharge <= 499) {
   $discharge_class = 'safe';
} elseif ($discharge >= 500 && $discharge <= 799) {
    $discharge_class = 'caution';
} elseif ($discharge >= 800) {
    $discharge_class = 'dangerous';
}

编辑:

这有点用。它以彩色输出放电数 框,而不是“娱乐活动 - 安全、谨慎或危险”。

然后将输出更改为不显示数字而是显示类值:

echo '<span class="discharge-'.$discharge_class($discharge).'">Recreational Activity - '.$discharge_class($discharge).'</span>';

它也没有使用最新的观察,如果你看一下xml, 有 48 小时的 obs。我只想要最新的 obs 用过。

然后获取数组中的最后一个元素,这是我能看到的最新的。

// last item
$discharge = $xml->RESULTSET[1]->ROW[count($xml->RESULTSET[1]->ROW)-1]->AVG_HOURLY_DISCHARGE;

试试这个:

<style>
    .discharge-safe {
        background: green;
        color: white;
    }

    .discharge-caution {
        background: yellow;
        color: white;
    }

    .discharge-dangerous {
        background: red;
        color: white;
    }
</style>
<?php
$url = "http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml";
$xml = simplexml_load_file($url);

// last item
$discharge = $xml->RESULTSET[1]->ROW[count($xml->RESULTSET[1]->ROW)-1]->AVG_HOURLY_DISCHARGE;

$discharge_range = [
    'safe'      => [ 'min_range' => 0, 'max_range' => 499 ], 
    'caution'   => [ 'min_range' => 500, 'max_range' => 799 ], 
    'dangerous' => [ 'max_range' => 800 ]
];

$discharge_class = function ($value) use ($discharge_range) {
    foreach ($discharge_range as $key => $range) {
        if (filter_var($value, FILTER_VALIDATE_INT, ['options' => $range]) !== false) {
            return $key;
        }
    }
};

echo '<span class="discharge-'.$discharge_class($discharge).'">Recreational Activity - '.$discharge_class($discharge).'</span>';
?>

【讨论】:

  • 这有点用。它在彩色框中输出放电编号,而不是“娱乐活动 - 安全、小心或危险”。它也没有使用最新的观察,如果你看一下xml,有48小时的obs。我只想使用最新的 obs。每小时都会使用该小时的最新 obs 更新 xml。
  • 感谢您的帮助和恶作剧!
猜你喜欢
  • 2022-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 2019-04-22
  • 2011-04-11
  • 1970-01-01
相关资源
最近更新 更多