mardi 24 avril 2018

foreach inside IF condition will not output, even when condition is TRUE

I've looked at this too long to be able to discern why it's not working. As stated in the comments of the sample file (link below) I can output the correct values for the variables used in the IF condition. But as soon as I wrap the foreach in the condition, followed by the ELSE, the condition is ignored and the ELSE statement is output.

I have tested this replacing either/both of the two IF variables with numbers. It works correctly in that case. But when it's the two variables, it won't. I am baffled and convinced it's something entirely basic and likely syntax which I'm blind to because I've tried to find it too many times ... I hope.

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 are up for a week.

Aucun commentaire:

Enregistrer un commentaire