【发布时间】: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