即使有更多数据要通过数组元素运行,循环也会中断

我有三个表,它们都包含有关公司不同合作伙伴的数据,已经有一个层次结构,销售合作伙伴在假日合作伙伴下,而这些又在超级合作伙伴下,所有这些数据都已经在数据库中,现在我需要在假期合作伙伴的仪表板上(他下属的销售合作伙伴的)显示销售合作伙伴进行的业务,超级合作伙伴也是如此。


问题是我无法检索有关层次结构的下一个后续级别的数据,例如,在假期合作伙伴的破折号上,我只能显示有关假期合作伙伴的数据,而不能显示他下的销售合作伙伴的数据。


已经尝试删除 foreach 循环,因为我知道它不必要地增加了复杂性,尝试保留 if-elseif 条件无济于事。


案例“假期伙伴”:


  //2nd level, need holiday as well as sales partners

  $case = 2;

   //holiday partner data, i.e getting to know which holiday partner we're talking about

  $sql2 = "SELECT sno, district, state FROM holidaypartners WHERE name = '$userid'";

  $res = $conn->query($sql2);

   if ($res->num_rows)

   {

     if($row = $res->fetch_assoc()){

      $sno = $row["sno"];

      $district = $row["district"];

      $state = $row["state"];

     }

   }

   echo"$sno - "; //this is getting printed

   //got sno of holiday partner

    $sql1= "SELECT * FROM agent_form_data

             WHERE sales_partner_name ='".$sno."' and formstatus = 'pending'";

    $res = $conn->query($sql1);

    //check for forms filled by the holiday partner himself

    if ($res->num_rows){

       while($row = $res->fetch_assoc()){

         $datesent =date_create($row["datesent"]);

         $datesent =date_format($datesent,"d-M-Y");

        echo "<tr>

                <td>GHRN".(5000+(int)$row["ref_num"])."</td>

                <td>".$row["cust_firstname"]." ".$row["cust_lastname"]."</td>

                <td>".$row["holi_dest"]."</td>

                <td>".$row["date_of_travel"]."</td>

                <td>".$row["return_date_of_travel"]."</td>

                <td>".$row["duration"]."</td>

                <td>".$datesent."</td>

                </tr>";

        }

    }

我没有收到任何错误,因为没有语法缺陷,但是,我应该能够循环浏览所有元素(假期合作伙伴下的合作伙伴的 ID)以获得所需的结果。


蝴蝶不菲
浏览 121回答 1
1回答

慕运维8079593

您需要更干净地编写代码。您将所有查询和结果命名为类似的覆盖。我试图更简洁地编写您的查询,以便您了解这个想法。我无法测试,因为我没有数据库,但这应该足以让你继续前进。$query_salespartners = "SELECT sno FROM salespartners WHERE holiday_partner_sno = '$sno'";$res_salespartners = $conn->query($query_salespartners);if ($res_salespartners->num_rows) {while($row = $res->fetch_assoc()){&nbsp; $val = $row['sno'];&nbsp; echo "$val "; //here only the first value is getting printed, suppose there are four sales partners under him, 6001,6002,6003,6004, now on execution, it prints only 6001, if there is no data to show it simply exits *all* the loops.&nbsp; $query_sales= "SELECT * FROM agent_form_data&nbsp; WHERE sales_partner_name ='".$val."' and formstatus = 'pending'";//on the other hand, if I remove the SQL part, all the ids under this holiday partner are getting printed, which proves that the array has it but SQL for some reason isn't checking all of the members of the array&nbsp; $res_sales = $conn->query($query_sales);&nbsp; if($res_sales->num_rows){&nbsp; &nbsp; echo"Hi!";&nbsp; &nbsp; while($row_sales = $res_sales->fetch_assoc()){&nbsp; &nbsp; &nbsp; echo $row_sales["ref_num"];&nbsp; &nbsp; &nbsp; $datesent =date_create($row_sales["datesent"]);&nbsp; &nbsp; &nbsp; $datesent =date_format($datesent,"d-M-Y");&nbsp; &nbsp; &nbsp; echo "<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>GHRN".(5000+(int)$row_sales["ref_num"])."</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>".$row_sales["cust_firstname"]." ".$row_sales["cust_lastname"]."</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>".$row_sales["holi_dest"]."</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>".$row_sales["date_of_travel"]."</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>".$row_sales["return_date_of_travel"]."</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>".$row_sales["duration"]."</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>".$datesent."</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>";&nbsp; &nbsp; }&nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP