php - 即使有更多数据要遍历数组元素也会中断循环

标签 php html mysql

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

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

已经尝试删除 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>";
        }
    }
     //sales partner data
    $sql = "SELECT sno FROM salespartners WHERE holiday_partner_sno = '$sno'";
    $res = $conn->query($sql);
    if ($res->num_rows){
    while($row = $res->fetch_assoc()){
              $nums = array_values($row); //converting into a normal array for the foreach loop
              foreach($nums as $val){
                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.

                $sql2= "SELECT * FROM agent_form_data
                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
                $res = $conn->query($sql2);
                if($res->num_rows){
                  echo"Hi!";
                  while($row = $res->fetch_assoc()){
                    echo $row["ref_num"];
                    $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>";
                  }
              }
              else{
                echo "No Data";
              }

              }
    }
   }
  break;

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

最佳答案

您需要更干净地编写代码。您以类似的方式命名所有查询和结果,从而导致覆盖。

我尝试更清晰地编写您的查询,以便您能够理解。我无法测试,因为我没有数据库,但这应该足以让你继续。

$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()){
  $val = $row['sno'];
  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.

  $query_sales= "SELECT * FROM agent_form_data
  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
  $res_sales = $conn->query($query_sales);
  if($res_sales->num_rows){
    echo"Hi!";
    while($row_sales = $res_sales->fetch_assoc()){
      echo $row_sales["ref_num"];
      $datesent =date_create($row_sales["datesent"]);
      $datesent =date_format($datesent,"d-M-Y");
      echo "<tr>
          <td>GHRN".(5000+(int)$row_sales["ref_num"])."</td>
          <td>".$row_sales["cust_firstname"]." ".$row_sales["cust_lastname"]."</td>
          <td>".$row_sales["holi_dest"]."</td>
          <td>".$row_sales["date_of_travel"]."</td>
          <td>".$row_sales["return_date_of_travel"]."</td>
          <td>".$row_sales["duration"]."</td>
          <td>".$datesent."</td>
          </tr>";
    }
  }
}
}

关于php - 即使有更多数据要遍历数组元素也会中断循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56217481/

相关文章:

php - 如何检测访问者的真实IP地址

不包括 <pre> 标记的 PHP 正则表达式

php - "Proper"index.php 和前端 Controller 的分离/区别

javascript - 滚动时淡出div并淡入其他div

php - 错误号 : 1064 CodeIgniter query builder

mysql - 如何在MySQL中保存一个汉字𥚃

php - Cloudfoundry 命令行工具不会推送所有文件

php - 如何通过单击提交按钮在 PHP 页面的同一链接上显示搜索数据?

sql - 运行前检查查询结果

html - 具有不规则形状的两个相连的div-带有文本的图像