如何使用两个while循环比较两个mysqli_fetch_assoc值

时间:2017-05-16 04:11:25

标签: php mysqli

这是我的代码......

while ($guest_orders=mysqli_fetch_assoc($result_guest_orders)) {
                                $temp_guest_prod=$guest_orders['prodCode'];
                                $temp_guest_qty=$guest_orders['qty'];
                                while ($logged_orders=mysqli_fetch_assoc($result_logged_orders)) {
                                    $temp_logged_prod=$logged_orders['prodCode'];
                                    echo $temp_guest_prod."<br />";
                                    if($temp_guest_prod==$temp_logged_prod){
                                        echo "same product<br />";
                                    }
                                    echo "logged prod code: ".$logged_orders['prodCode']."   QUANTITY: ".$logged_orders['qty']."<br />";
                                }
                                $logged_orders="";
                                echo "temp prod code: ".$temp_guest_prod."   QUANTITY: ".$temp_guest_qty."<br />";
                                echo "guest prod code: ".$guest_orders['prodCode']."   QUANTITY: ".$guest_orders['qty']."<br />";
                            }

这是输出......

enter image description here

我对这种情况有点困惑。我需要比较两个字段,以了解它们是否具有相同的值。有人可以向我解释一下吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

您可以使用:

while ($guest_orders=mysqli_fetch_assoc($result_guest_orders)) {

  $temp_guest_prod=$guest_orders['prodCode'];
  $temp_guest_qty=$guest_orders['qty'];

  echo "Begin ".$temp_guest_prod." comparing :";

  while ($logged_orders=mysqli_fetch_assoc($result_logged_orders)) {

    $temp_logged_prod=$logged_orders['prodCode'];
    $temp_logged_prod_qty=$logged_orders['qty'];
    echo "Comparing ".$temp_guest_prod." to ".$temp_logged_prod.":<br />";
    //You can also compare name and quantity:
    //if(($temp_guest_prod==$temp_logged_prod) && ($temp_guest_qty==$temp_logged_prod_qty)){ run code... }
    if($temp_guest_prod==$temp_logged_prod){
      echo "Same product<br />"; 
      $logged="YES"; 
      } else { 
      "Not the same<br />"; 
      }

    echo "logged prod code: ".$temp_logged_prod."   QUANTITY: ".$temp_logged_prod_qty."<br />";
   }

    $logged="";

    echo "temp prod code: ".$temp_guest_prod."   QUANTITY: ".$temp_guest_qty."<br />";
    echo "guest prod code: ".$guest_orders['prodCode']."   QUANTITY: ".$guest_orders['qty']."<br />";

    echo "End of product ".$temp_guest_prod." comparing. <br /> Result:";

    if($logged=="YES"){
      echo "Match found!<br /><br />";
      } else { 
      echo "No match.<br /><br />";
      }
}

答案 1 :(得分:0)

感谢您的快速回复和友好的帮助。但我现在已经知道了这个问题,并得到了一个成功的答案。以下是我的解决方案。

while ($guest_orders=mysqli_fetch_assoc($result_guest_orders)) {
                                $temp_guest_prod[]=$guest_orders;
                            }

                            while ($logged_orders=mysqli_fetch_assoc($result_logged_orders)) {
                                $temp_logged_prod[]=$logged_orders;
                            }

                            foreach($temp_guest_prod as $guest_orders) {
                                foreach($temp_logged_prod as $logged_orders) {
                                    if ($guest_orders['prodCode'] == $logged_orders['prodCode']){
                                        echo "guest prod code: ".$guest_orders['prodCode']."   QUANTITY: ".$guest_orders['qty']."<br />";
                                        echo "logged user prod code: ".$logged_orders['prodCode']."   QUANTITY: ".$logged_orders['qty']."<br />";
                                    }

                                }

                            }

还是谢谢你!