【发布时间】: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; ?>
<!-- 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