嵌套的foreach循环不输出数据

时间:2016-03-14 00:38:16

标签: php mysql

我试图根据两个不同的查询结果在while循环中运行foreach循环。 while循环完全迭代,但我没有从嵌套的foreach循环中获得任何输出。有任何想法吗?我知道它不是mysqli,我正在进行更新。

if ($con) {
    $sql = "SELECT Flights.TeamID, Flights.FlightID, Flights.MemberID, Flights.DepDate, Flights.DepTime, Flights.DepPoint, Flights.DepCountry, Flights.ArrDate, Flights.ArrTime, Flights.ArrPoint, Flights.ArrCountry, Flights.Layover, Flights.Airline, Flights.FlightNo, Members.MemberID FROM Flights LEFT JOIN Members ON Flights.MemberID=Members.MemberID ORDER BY DepDate ASC, DepTime";
    $result = mysql_query($sql) or die(mysql_error());

    $passengers = array();
    $sql2 = "SELECT Members.MemberID, MemberAdmin.IndID, MemberAdmin.First, MemberAdmin.Last From Members LEFT JOIN MemberAdmin ON Members.IndID=MemberAdmin.IndID";
    $result2 = mysql_query($sql2) or die(mysql_error());
    while($row2=mysql_fetch_array($result2)) {
       $passengers[$row2["MemberID"]] = $row2;
    }

    if ($result) {

        // output data of each row from query1.

        echo "<table><thead style='white-space: nowrap; padding: 10px;'><tr><th width='160px'></th><th width='170px' style='text-decoration:underline; margin-right: 0;'>Flight:</th><th width='170px' style='text-decoration:underline;'>Dep Time:</th><th width='170' style='text-decoration:underline; margin-right: 0;'>Dep Point:</th><th width='170px' style='text-decoration:underline; margin-right: 0;'>Arr Time:</th><th width='170px' style='text-decoration:underline; margin-right: 0;'>Arr Point:</th></tr><tbody>";
        $trdate=null;
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $da = strtotime($row["ArrDate"]);
            $dd = strtotime($row["DepDate"]);

            if($trdate != $row['DepDate']){
                $trdate = $row['DepDate'];
                echo "<tr class='spacer'></tr><tr><td style='white-space: nowrap; padding: 10px;'><b>".date('D - dS',strtotime($row['DepDate']))."</b></td></tr>\r\n";
            }
            echo "<tr><td></td><td>". $row["Airline"]. " ". $row["FlightNo"]. "</td><td>". date("g:i A",strtotime($row["DepTime"])). "</td><td>". $row["DepPoint"]. ", " . $row["DepCountry"]. "</td><td>". date("g:i A",strtotime($row["ArrTime"])). "</td><td>".  $row["ArrPoint"]. ", " . $row["ArrCountry"]. "</td>";

            // output conditional data from query2.

            $pass = $row["MemberID"];
            foreach($passengers as $passenger){
                if (isset($passenger[$pass])) {
                    echo'<tr><td></td><td>'.$passenger["First"]. ', '. $passenger["Last"]. '</td></tr>';
                }
            }
        }

        echo "</tbody></table>";
    } else {
        echo "0 results";
    }
    }
    else {
        echo'Not connected to database';
    }

2 个答案:

答案 0 :(得分:0)

foreach($passengers as $pasenger){
    if (isset($passenger[$pass])) {
        echo'<tr><td></td><td>'.$passenger["First"]. ', '. $passenger["Last"]. '</td></tr>';
    }
 }

应该是

foreach($passengers as $passenger){
    if (isset($passenger[$pass])) {
        echo'<tr><td></td><td>'.$passenger["First"]. ', '. $passenger["Last"]. '</td></tr>';
    }
 }

答案 1 :(得分:0)

感谢所有花时间看这个的人!问题最终是将MemberID不正确地创建到数据库中。代码现在工作正常。