I'm comparing 2 numbers entered to numbers in an xml file and then storing the result of each comparison in a variable ($n1, $n2). I'm then adding the total of each variable up and storing it in a new variable $total within the foreach loop. I've tried displaying the result in the loop but it shows both the result for correct and incorrect comparison as it loops around the whole xml.
I want to be able to use $total outside of the loop to display a result. When I echo $total outside the loop, its result is 0 no matter whether the comparisons are correct. When I echo it in the loop it has a value.
How to I ensure the $total variable keeps its stored value outside of the loop?
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$xml = simplexml_load_file('lottery2.xml') or die("Error: Cannot create object");
if(isset($_POST['num1'])&& isset($_POST['num2']))
{
foreach($xml->children() as $record)
{
if($record->num1 == $num1 || $record->num2 == $num1)
{
$n1=1;
}
else
{
$n1=0;
}
if($record->num1 == $num2 || $record->num2 == $num2)
{
$n2=1;
}
else
{
$n2=0;
}
$total= $n1+$n2;
}//end foreach
if ($total=2) {
echo "Jackpot is ".$record->jackpot ."<br />";
} else {
echo "No jackpot, sorry";
}