【问题标题】:while-Loop in foreach-Loop with mysqli_query leads to unexpected result带有 mysqli_query 的 foreach-Loop 中的 while-Loop 导致意外结果
【发布时间】:2019-01-28 19:39:37
【问题描述】:

我有一段代码可以从 mysql 数据库中获取数据。我需要为具有相同 station_group 的所有站点构建容器,我通过 foreach 循环执行此操作。在 foreach 循环中,有一个 while 循环将所有具有父 station_group 的站填充到站组容器中。如果我在代码中调试回显行(因此存在某种延迟),则代码可以正常工作,但是将它们注释掉,代码会提供错误的容器和站点顺序。我猜它是因为异步函数 fetch_assoc 所以我可能会放入一个回调函数,但我只是不让它运行。因此,我会给予任何帮助... =)

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
        }
        ?>

【问题讨论】:

  • 我的电脑无法生成“shit”,所以很难重现。有味道吗?

标签: php asynchronous mysqli foreach while-loop


【解决方案1】:

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

存储上一个 station_group 并检查 current_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>';
?>

【讨论】:

  • 感谢您的快速回复。 One Loop 是不够的,因为它不会在相应的容器内交付站点。
【解决方案2】:

看起来将结果简单地转储到数组中然后循环遍历数组可能更容易。

<?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>';
        }
        ?>

【讨论】:

  • 我在想以这种方式回答,但最终决定这不是问题的答案。
  • 非常感谢!代码现在在代码行“while ($rows as $row) {”处提供错误:“Parse error: syntax error, unexpected 'as' (T_AS) in C:\xampp\htdocs\index.php”。我在那个地方尝试了一个 foreach 循环来保留原始问题
猜你喜欢
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多