dpvmjk0479 2016-08-11 11:23
浏览 104
已采纳

使用if语句检查xml文件中是否存在数字?

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";
                }
  • 写回答

1条回答 默认 最新

  • dousong8187 2016-08-11 12:35
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟