在带有mysqli_query的foreach-loop中使用while-Loop会导致意外结果

时间:2019-01-28 19:39:38

标签: php asynchronous mysqli foreach while-loop

我有一些代码可以从mysql数据库中获取数据。我需要通过foreach循环为具有相同station_group的所有站点构建容器。在foreach循环内,有一个while循环,用所有具有父station_group的工作站填充工作站组容器。如果我对代码中的回声行进行了调试(这样有些延迟),则该代码可以正常工作,但是将其注释掉后,该代码将错误地指示容器和站的顺序。我猜测它是由于异步函数fetch_assoc造成的,所以我可以放入一个回调函数,但是我只是不知道它是runnig。因此,我将寻求任何帮助... =)

BR

<?php

        //build unique Station group array
        $sql_unique = "SELECT DISTINCT station_group FROM station ORDER BY station_group ASC";
        $unique_station_groups = mysqli_query($dbConn, $sql_unique);

        //get all station data
        $sql = "SELECT * FROM station ORDER BY station_group, station_id ASC";
        $result= mysqli_query($dbConn, $sql);

        //Loop for station_group
        foreach ($unique_station_groups as $station_group_value){
            echo '<div class="css-station-group>';
            while ($row = mysqli_fetch_assoc($result)) {

                //echo "<script>console.log(".json_encode($station_group_value).")</script>";
                //echo "<script>console.log(".json_encode($row).")</script>";
                $station_id = $row['station_id'];
                $station_name = $row['station_name'];
                $station_layout = $row['station_layout'];
                $station_group = $row['station_group'];

                if ($station_group_value['station_group']==$station_group) {
                    echo '<div class="station-container css_station-layout-'.$station_layout.
                    '" id='.$station_id.
                    '>'.$station_name.
                    '<br></div>';
                }
            }
            echo '</div>';
            mysqli_data_seek($result,0); //reset array, so next Loop will find values again
        }
        ?>

2 个答案:

答案 0 :(得分:0)

将结果简单地转储到数组中然后遍历数组似乎更容易。

<?php

        //build unique Station group array
        $sql_unique = "SELECT DISTINCT station_group FROM station ORDER BY station_group ASC";
        $unique_station_groups = mysqli_query($dbConn, $sql_unique);

        //get all station data
        $sql = "SELECT * FROM station ORDER BY station_group, station_id ASC";
        $result= mysqli_query($dbConn, $sql);

        $rows = [];
        while ($row = mysqli_fetch_assoc($result)) {
            $rows[] = $row;
        }
        //Loop for station_group
        foreach ($unique_station_groups as $station_group_value){
            echo '<div class="css-station-group>';
            foreach ($rows as $row) {

                //echo "<script>console.log(".json_encode($station_group_value).")</script>";
                //echo "<script>console.log(".json_encode($row).")</script>";
                $station_id = $row['station_id'];
                $station_name = $row['station_name'];
                $station_layout = $row['station_layout'];
                $station_group = $row['station_group'];

                if ($station_group_value['station_group']==$station_group) {
                    echo '<div class="station-container css_station-layout-'.$station_layout.
                    '" id='.$station_id.
                    '>'.$station_name.
                    '<br></div>';
                }
            }
            echo '</div>';
        }
        ?>

答案 1 :(得分:0)

您不需要两个查询即可执行此操作。

存储上一个station_group,并检查current_group和上一个group来区分工作站。

稍微更改了代码

<?php
    //build unique Station group array
    // $sql_unique = "SELECT DISTINCT station_group FROM station ORDER BY station_group ASC";
    // $unique_station_groups = mysqli_query($dbConn, $sql_unique);

    //get all station data
    $sql = "SELECT * FROM station ORDER BY station_group ASC";
    $result= mysqli_query($dbConn, $sql);

    $rows = [];
    while ($row = mysqli_fetch_assoc($result)) {
        $rows[] = $row;
    }
    //Loop for station_group
    // foreach ($unique_station_groups as $station_group_value) {
    $current_station_group = null;
    echo '<div class="css-station-group">';
    foreach ($rows as $row) {

        //echo "<script>console.log(".json_encode($station_group_value).")</script>";
        //echo "<script>console.log(".json_encode($row).")</script>";
        $station_id = $row['station_id'];
        $station_name = $row['station_name'];
        $station_layout = $row['station_layout'];
        $station_group = $row['station_group'];

        if ($station_group != $current_station_group) {
            echo '<div class="station-container css_station-layout-'.$station_layout.
            '" id='.$station_id.
            '>'.$station_name.
            '<br></div>';
            $current_station_group = $row['station_group'];
        }
    }
    echo '</div>';
?>