【问题标题】:foreach inside IF condition will not output, even when condition is TRUEIF 条件中的 foreach 不会输出,即使条件为 TRUE
【发布时间】:2018-10-05 05:45:51
【问题描述】:

我看这个太久了,无法辨别为什么它不起作用。如示例文件的 cmets 中所述(下面的链接),我可以为 IF 条件中使用的变量输出正确的值。但是只要我将 foreach 包装在条件中,然后是 ELSE,就会忽略该条件并输出 ELSE 语句。

我已经测试了用数字替换两个 IF 变量中的一个/两个。在这种情况下它可以正常工作。但是当它是两个变量时,它不会。我很困惑,并确信这是一种完全基本且可能的语法,我对此视而不见,因为我试图找到它太多次了......我希望。

PHP:https://pastebin.com/2ZGTPjs0

$xml = simplexml_load_file('file.xml');

$expenseCount = $xml->EXP->count();
$grandTotal = $xml->GRANDTOTAL;
$annualMax = $xml->ANNUALMAX;

// xPath is correct. The below test outputs the correct numbers.
// echo "<h1>GRAND TOTAL: ".$grandTotal."</h1><br/><h1>ANNUAL MAX: ".$annualMax."</h1>";

// The below if condition seems to be ignored and I can't figure out why!!
// Even though $grandTotal IS GREATER THAN $annualMax, the else statement
if ($grandTotal > $annualMax) {

foreach($xml->EXP as $expenses) {

echo "<div class=\"coupon ".$i."\">";
echo "<h1>Reimbursement</h1><h2>Coupon No. ".$i." of ".$expenseCount."</h2>";

echo "<table>
<tbody>
<tr class=\"headings\">
<th>Service</th>
<th>Vendor</th>
<th>Date of Service</th>
<th>Reason</th>
<th>Total Cost</th>
<th>Amount Paid</th>
<th>Balance Due</th>
</tr>";
echo "<tr class=\"expense-details\">";
echo "<td>".$expenses->SERVICE."</td>";
echo "<td>".$expenses->VENDOR."</td>";
$serviceDate = date('M d, Y', strtotime($expenses->DATE));
echo "<td>".$serviceDate."</td>";
echo "<td>".$expenses->REASON."</td>";
echo "<td>$".$expenses->COST."</td>";
echo "<td>$".$expenses->PAID."</td>";
echo "<td>$".$expenses->BALANCE."</td>";
echo "</tr>
</tbody>
</table>";

echo "</div>";

$i++;
}
} else {
echo "<div class=\"not-yet\">";
echo "<h1>Your current total is $".$grandTotal.". You have not exceeded the annual minimum out-of-pocket expenses of $".$annualMax.".</h1>";
echo "</div>";
}

XML:https://pastebin.com/3Md8w93J

<EXPREP>
<GRANDTOTAL>727.94</GRANDTOTAL>
<ANNUALMAX>714</ANNUALMAX>
<EXP>
<DATE>Fri Jan 12 2018 00:00:00 GMT-0500 (EST)</DATE>
<SERVICE>Repair</SERVICE>
<VENDOR>ABC Auto Body</VENDOR>
<REASON>Out of Alignment</REASON>
<COST>130</COST>
<PAID>110</PAID>
<BALANCE>20</BALANCE>
</EXP>
<EXP>
<DATE>Tue Jan 23 2018 00:00:00 GMT-0500 (EST)</DATE>
<SERVICE>Repair</SERVICE>
<VENDOR>XYZ Gas Station</VENDOR>
<REASON>Travel Gas</REASON>
<COST>212</COST>
<PAID>192</PAID>
<BALANCE>20</BALANCE>
</EXP>
<EXP>
<DATE>Tue Jan 23 2018 00:00:00 GMT-0500 (EST)</DATE>
<SERVICE>Radiator Flush</SERVICE>
<VENDOR>Quick Flush</VENDOR>
<REASON>Annual Radiator Maintenance</REASON>
<COST>101.12</COST>
<PAID>91.12</PAID>
<BALANCE>10</BALANCE>
</EXP>
<EXP>
<DATE>Fri Jan 26 2018 00:00:00 GMT-0500 (EST)</DATE>
<SERVICE>Oil Change</SERVICE>
<VENDOR>ABC Auto Body</VENDOR>
<REASON>3000 Mile Oil Change</REASON>
<COST>125</COST>
<PAID>105</PAID>
<BALANCE>20</BALANCE>
</EXP>
</EXPREP>

Pastebins 可以使用一周。

【问题讨论】:

  • 不要使用 pastebin。在您的问题中包含相关材料,以便将来寻找类似答案的人仍然有意义。
  • 我使用了 pastebins,因为它们有一个原始视图,可以容纳 php 打开/关闭标签并且易于复制/粘贴。另请注意,我确实在问题正文中包含了相关材料...
  • 不直接相关,但这不是产生词法 XML 输出的方式。您没有尝试转义出现在数据中的特殊字符,因此一旦您的数据包含 &amp;&lt;,您将生成无效的 XML。
  • @MichaelKay 虽然我很欣赏这种担忧,但这并不是真正关于生成 XML 输出。它是关于在 PHP 中使用 XML 数据源。也就是说,XML 输出是由 Google Apps 脚本生成的,该脚本将 Google 表格的行和列转换为有效的 XML。刚刚测试了您的关注点,它在节点内容中正确编码了 html 实体。

标签: php xml if-statement foreach conditional-statements


【解决方案1】:

尝试将变量转换为浮点数。就目前而言,它们是 SimpleXML 对象,可能会打乱您的比较:

$grandTotal = (float) $xml->GRANDTOTAL; $annualMax = (float) $xml->ANNUALMAX;

【讨论】:

  • 非常感谢。谢谢。
猜你喜欢
  • 2017-11-12
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
相关资源
最近更新 更多