A.Hollingsworth A.Hollingsworth - 3 months ago 20
PHP Question

Using if statement to check if numbers exist in xml file?

I have a form which users enter 6 numbers then I'm comparing the entered numbers to xml elements using SimpleXML. If the correct number is found, then it gets stored in a variable($n1 - $n6). The variables are used outside the foreach loop to determine if all 6 numbers are correct, then echos the date and jackpot of the element. This works correctly.

If the numbers entered do not match the xml elements, I want to echo an incorrect result ("Sorry"). However, this part doesn't work as it still echos a date and jackpot which isnt linked the the numbers in xml. It results in a random date and jackpot number which aren't store in the xml file.

How would I ensure when 6 incorrect numbers are entered the result is wrong and echo "Sorry"?

XML:

<lottery>
<number>
<date>7.11.2015</date>
<num1>2</num1>
<num2>3</num2>
<num3>34</num3>
<num4>43</num4>
<num5>49</num5>
<num6>50</num6>
<jackpot>13156375</jackpot>
</number>
<number>
<date>08.11.2015</date>
<num1>11</num1>
<num2>13</num2>
<num3>18</num3>
<num4>25</num4>
<num5>29</num5>
<num6>47</num6>
<jackpot>8842605</jackpot>
</number>
</lottery>


PHP:

$n1 = false;//make true once we find 1st num
$n2 = false;//make true once we find 2nd num
$n3 = false;
$n4 = false;
$n5 = false;
$n6 = false;

$xml = simplexml_load_file('lottery2.xml') or die("Error: Cannot create object");
if (isset($_POST['num1']) && isset($_POST['num2'])) {

foreach ($xml->children() as $record) {
$n1 = $n1 || $record -> num1 == $num1 || $record -> num2 == $num1 || $record -> num3 == $num1 || $record -> num4 == $num1 || $record -> num5 == $num1 || $record -> num6 == $num1;
$n2 = $n2 || $record -> num1 == $num2 || $record -> num2 == $num2 || $record -> num3 == $num2 || $record -> num4 == $num2 || $record -> num5 == $num2 || $record -> num6 == $num2;
$n3 = $n3 || $record -> num1 == $num3 || $record -> num2 == $num3 || $record -> num3 == $num3 || $record -> num4 == $num3 || $record -> num5 == $num3 || $record -> num6 == $num3;
$n4 = $n4 || $record -> num1 == $num4 || $record -> num2 == $num4 || $record -> num3 == $num4 || $record -> num4 == $num4 || $record -> num5 == $num4 || $record -> num6 == $num4;
$n5 = $n5 || $record -> num1 == $num5 || $record -> num2 == $num5 || $record -> num3 == $num5 || $record -> num4 == $num5 || $record -> num5 == $num5 || $record -> num6 == $num5;
$n6 = $n6 || $record -> num1 == $num6 || $record -> num2 == $num6 || $record -> num3 == $num6 || $record -> num4 == $num6 || $record -> num5 == $num6 || $record -> num6 == $num6;

if ($n1 && $n2 && $n3 && $n4 && $n5 && $n6) break; //Found match. No need to keep looking

}//end foreach

if ($n1 && $n2 && $n3 && $n4 && $n5 && $n6)
{
echo $record -> date;
echo $record -> jackpot;
}
else
{
echo "Sorry no winnings";
}

Answer

If think you should consider using arrays instead of bulky if statements.

I would do it this way:

  • build the list of player numbers and sort it
  • for each entry in the XML file:
    • build the list of lottery numbers and sort it
    • compare the player numbers with the lottery numbers, using the sorted arrays

Example code:

$xml = simplexml_load_file('lottery2.xml') or die("Error: Cannot create object");

if(isset($_POST['num1']) && isset($_POST['num2'])) {
  $myList = array($num1, $num2, $num3, $num4, $num5, $num6);
  $result = array();
  $win = 0;

  sort($myList);

  foreach ($xml->children() as $record) {
    for($n = 0; $n < 6; $n++) {
      $result[$n] = (int)$record -> {'num'.($n + 1)};
    }
    sort($result);

    if (match($myList, $result)) {
      echo $record -> date;
      echo $record -> jackpot;
      $win++;
    }
  }
  if($win == 0) {
    echo "Sorry no winnings";
  }
}

function match ($list1, $list2) {
  for($n = 0; $n < 6; $n++) {
    if($list1[$n] != $list2[$n]) {
      return false;
    }
  }
  return true;
}
Comments