【发布时间】:2020-10-07 05:08:19
【问题描述】:
我正在使用简单 XML 将选举结果数据格式化为用户友好的网页。我正在使用 PHP foreach 列出比赛和候选人,但是在每个被视为候选人的比赛下嵌套了一些额外的数据(准确地说是 7 个元素)。我怎样才能过滤掉它们?
这是 XML:https://ftpcontent.worldnow.com/wicu/BTI/election.xml
这是代码输出:https://lillydigitalmedia.com/election.php
代码如下:
<?php
$btiresults = simplexml_load_file("https://ftpcontent.worldnow.com/wicu/BTI/election.xml");
echo "<p><em>Updated: " . $btiresults['Time'] . "</em></p>";
foreach($btiresults->children() as $race) {
echo "<h4 class='card-title' style='margin-top: 20px'><strong>" . $race->Name1 . "</strong></h4>";
foreach($race->children() as $candidate) {
echo "<p style='width: 100%;'><span style='float: left;'><strong>" . $candidate->Name . "</strong> <small>" . $candidate->Party . "</small></span><span style='float: right;'>" . $candidate->Votes . " votes</span></p>";
echo "<div class='progress' style='height: 25px; width: 100%;'>";
echo "<div class='progress-bar' role='progressbar' style='width:" . $candidate->PercentageOfVotesCast . "%' aria-valuenow='" . $candidate->PercentageOfVotesCast . "' aria-valuemin='0' aria-valuemax='100'>" . $candidate->PercentageOfVotesCast . "%</div>";
echo "</div>";
}
}
?>
提前致谢。
【问题讨论】:
-
到目前为止你尝试过什么?为什么不简单地检查它们并做点什么?
-
您好,请问您能否在edit 问题中包含示例 XML 和输出在问题本身中。我们不仅不需要单击随机链接来帮助您,而且它们将来很有可能会停止工作,从而使这个问题对其他有同样问题的人毫无用处。