While Loop未显示正确的数据


While Loop not displaying correct data

而PHP中的循环没有显示正确的数据。我正试图用SQL数据库填充我的菜单。它只给我DB事件的最后一个条目,尽管DB中有6个条目。

这是我的代码:

$top_sql = "SELECT * FROM menu_top_level /* WHERE top_level_visible = 'Yes' */ ORDER BY 
                  top_level_order ASC";                                               // create a database query
                $top_res = sqlsrv_query($conn, $top_sql) or die(sqlsrv_error($conn)); // check connection and execute query
                if ($top_res = sqlsrv_query($conn, $top_sql)) {                     // if the query contains results...
    Here    >>>>>>>> while ($row = sqlsrv_fetch_array($top_res)) {                     // loop through each given row
                        $menu_block = "<div id='Accord' class='accord'><ul id='menu'>"; 
                        $menu_block .= "<li id='menubar'><a>".$row['2']."</a>";
                 /* echo "<pre>";
                    print_r($row);
                    echo "</pre>";*/
                        // Start: Build mid-level
                        $mid_sql = "SELECT * FROM menu_mid_level WHERE mid_level_fk_id = $row[top_level_pk_id] /* AND 
                          mid_level_visible = 'Yes' */ ORDER BY mid_level_order ASC"; // create a database query
                        $mid_res = sqlsrv_query($conn, $mid_sql, array(), array("Scrollable"=>"buffered")) or die(sqlsrv_error($conn));
                        $mid_num_rows = sqlsrv_num_rows ($mid_res); // get number of row
                        //echo $mid_num_rows;
                        $mid_num_rows_constant = $mid_num_rows;    // store number of row as a value
                        $mid_num_rows_counter = 0;                 // counter to match constant by adding 1 each loop (line 32)
                        if ($mid_num_rows == 0) {                  // unless if row count equals 0
                            $menu_block .= "</li>";                // close LI
                        } else if ($mid_num_rows > 0) {            // otherwise if more than 0...
                            $menu_block .= "<ul class='sub'>";                 // open UL for coming LI rows
                        }
                        if ($mid_res = sqlsrv_query($conn, $mid_sql)) {            // if the query contains results...
                            while ($row = sqlsrv_fetch_array($mid_res)) {            // loop through each given row
                                $menu_block .= "<li id='menubar'><a href='$row[mid_level_url]'>".$row[mid_level_name]."</a>";
                                $mid_num_rows_counter = $mid_num_rows_counter + 1;   // mark row count as handled (line 60)
                                // Start: Build bot-level
                                $bot_sql = "SELECT * FROM menu_bot_level WHERE bot_level_fkt_id = $row[mid_level_fk_id] 
                                  AND bot_level_fkm_id = $row[mid_level_order] /* AND bot_level_visible = 'Yes' */ 
                                  ORDER BY bot_level_order ASC";
                //echo $bot_sql;              // create a database query
                                $bot_res = sqlsrv_query($conn, $bot_sql, array(), array("Scrollable"=>"buffered")) or die(sqlsrv_error($conn));
                                $bot_num_rows = sqlsrv_num_rows ($bot_res);   
                 //echo $bot_num_rows;              // check number of rows
                                if ($bot_num_rows == 0) {                        // if no inner rows...
                                    $menu_block .= "</li>";                      // close above LI
                                } else if ($bot_num_rows > 0) {                  // otherwise if more than 0, it does contain...
                                    $menu_block .= "<ul class='sub'>";                       // so open UL to contain coming LI
                                }
                                if ($bot_res = sqlsrv_query($conn, $bot_sql)) {    // if the query contains results...
                                    while ($row = sqlsrv_fetch_array($bot_res)) {    // loop through each given row
                                        $menu_block .= "<li id='menubar'><a href='$row[bot_level_url]'>".$row[bot_level_name]."</a>
                                          </li>";                                    // and output row result
                                        $bot_num_rows = $bot_num_rows - 1;           // keep counting back 1 each loop until...
                                        if ($bot_num_rows == 0) {                    // check it is exactly 0...
                                            $menu_block .= "</ul></li>";             // row has ended so close UL and LI
                                        }
                                    }
                                } // End: Build bot-level
                            }
                        } // End: Build mid-level
                        // if reached last row & provided there was more than 1 row, then close UL and LI
                        if ($mid_num_rows_counter == $mid_num_rows_constant && $mid_num_rows_counter > 1) {
                            $menu_block .= "</ul></li></ul></div>";
                        }
                    }
                } // End: Build top-level

`

我认为您的错误在这一行:

$menu_block = "<div id='Accord' class='accord'><ul id='menu'>"; 

对于循环中的每一项,变量$menu_block都将重置为固定值
将该行更改为:

$menu_block .= "<div id='Accord' class='accord'><ul id='menu'>"; 

(注意.=)
您可能需要在循环上方的某个位置添加$menu_block='';

我发现了问题。

我的if语句论点不正确。循环到达第二个循环后停止。因为我检查循环是否应该退出的关闭if语句被设置为$variable>1而不是>=,所以它达到了1,它看到变量等于值1并退出

我替换了这个:

if ($mid_num_rows_counter == $mid_num_rows_constant && $mid_num_rows_counter > 1) {
                        $menu_block .= "</ul></li></ul></div>";
                    }

有了这个:

if ($mid_num_rows_counter == $mid_num_rows_constant && $mid_num_rows_counter >= 1) {
                        $menu_block .= "</ul></li></ul>";
                    }