php
  • html
  • mysql
  • 2016-08-31 28 views 1 likes 
    1

    我已經有一個while循環顯示我的表單中的選擇輸入,具體取決於用戶預訂的乘客數量。每個下拉菜單都應該顯示來自數據庫中列的所有數據。問題是,只有第一個下拉顯示我拿來的數據是座位1至30使用while循環的數據庫中的多個值下拉多個html

    查詢來獲得當前飛行的航空公司ID

    $result = mysqli_query($conn,"select * from flight_seat_status where flight_number = '$_SESSION[departure]'"); 
    

    變量我曾經開始計數循環

    $print = 1; $name = 0; 
    

    我while循環的問題

    echo "<h1 class='adminContent_h1'>Please choose a seat</h1>"; 
        while($print <= $_SESSION['passengers']){ 
        echo "<label style = 'width:170px;'>". $_SESSION['firstname'][$name]."'s Seat</label>"; 
        echo "<select class = 'content_dropdown' style = 'width:15%; margin-bottom:20px;' name = 'passengers[]'>"; 
        while($row = mysqli_fetch_assoc($result)){ 
        echo "<option value='".$row['seat_id']."'>".$row['seat_number']."</option>"; 
         } 
        echo "</select><br>"; 
         $print++; 
         $name++; 
        } 
    

    它能夠根據用戶打印出多個下拉菜單,但只有第一個下拉菜單包含數據。

    我的代碼有什麼問題?請詳細解釋

    +0

    您需要從數據庫中的數據存儲在一個額外的陣列使用的結果。 mysqli_fetch_assoc()只能在_once_上迭代。 – bwoebi

    +1

    bwoebi的權利。試試像這樣:'$ arr = array(); while($ row = mysqli_fetch_assoc($ result)){array_push($ arr,$ row);}'然後像這樣訪問所有東西:'for($ i = 0; $ i '。$ arr [$ i] [「seat_number」]。'';}' – cthefinal

    +0

    非常感謝生病嘗試,也 – TheAsker101

    回答

    0

    必須設置指針回到0再次

    // set the pointer back to the beginning 
        mysqli_data_seek($result, 0); 
        while($row = mysqli_fetch_assoc($result)){ 
        // do whatever here... 
        } 
    
    +0

    更有意義,並節省了我的線。謝謝 – TheAsker101

    0

    請使用下面的代碼。所有座位記錄您必須在乘客循環外取一次並存儲在另一個陣列中,然後每次都必須使用乘客循環內的座位排列。

    echo "<h1 class='adminContent_h1'>Please choose a seat</h1>"; 
    $seats = []; 
    while($row = mysqli_fetch_assoc($result)){ 
        $seats[] = $row; 
    } 
    while($print <= $_SESSION['passengers']){ 
        echo "<label style = 'width:170px;'>". $_SESSION['firstname'][$name]."'s Seat</label>"; 
        echo "<select class = 'content_dropdown' style = 'width:15%; margin-bottom:20px;' name = 'passengers[]'>"; 
        foreach($seats as $res_seat){ 
         echo "<option value='".$res_seat['seat_id']."'>".$res_seat['seat_number']."</option>"; 
        } 
        echo "</select><br>"; 
        $print++; 
        $name++; 
    } 
    
    相關問題