doulongsi1831 2016-01-03 07:40
浏览 8
已采纳

从循环中取出正确的价值

Below you can see my code. No need to show the other parts or the database table.

Code

    <form method="POST" action="custInfo.php">
<center><input type="submit" id="btn" name="room"></center>
$query = mysqli_query($conn, "SELECT room_name, room_type, room_rate, inclusive, description, room_status, max_cap
                                    FROM room
                                    WHERE room_status = 'available'");


if(mysqli_num_rows($query)){
    while($row = mysqli_fetch_assoc($query)){
        echo "<div class='chooseRoom'>";
        echo "<div class='indent'>";
        echo "<h4>".$row['room_type']."</h4>
            <h5>".$row['room_rate']."php</h5>";
        echo "<img src='../images/".$row['room_type'].".jpg' width=250 height=160>";
        echo "<h5>Number of Adult&nbsp;";
        echo "<select name='adult'>";
        $x = 0;
        while($x <= $row['max_cap']){
        echo "<option>";
        echo $x;
        echo "</option>";
        $x++;
        }
        echo "</select>";
        echo "&nbsp;&nbsp;&nbsp;Number of Children&nbsp;";
        echo "<select name='children'>";
        echo "<option>0</option>";
        echo "<option>1</option>";
        echo "<option>2</option>";
        echo "<option>3</option>";
        echo "<option>4</option>";
        echo "<option>5</option>";
        echo "</select></h5>";
        echo "<table class='table'>";
        echo "<thead>";
        echo "<th>Inclusive</th><th>Description</th>";
        echo "</thead>";
        echo "<tbody>";
        echo "<td>".$row['inclusive']."</td><td>".$row['description']."</td>";
        echo "</tbody>";
        echo "<tfoot>";
        echo "<td colspan='2'>";
        echo $row['room_status'];
        echo "</td>";
        echo "</tfoot>";
        echo "</table>";
        echo "</div>";
        echo "</div>";
    }
}
else{
    echo "No rooms available in the date you desire!";
}
        </form>

I need to take the value from the correct input. This code always takes the last input from the loop. And I have been trying to research about this for a day now. How do I fix this?

  • 写回答

2条回答 默认 最新

  • dongyan7950 2016-01-03 08:22
    关注

    Please follow the below code:

    $result = mysqli_query($connection, $query);
    
    ?>
    <form method="POST" action="custInfo.php">
        <center><input type="submit" id="submit_button" name="room_submit" value="Submit"></center>
        <?php
        while ($row = mysqli_fetch_assoc($result)) {
            ?>
        <div class='chooseRoom'>
            <div class='indent'>
                <h4><?php echo $row['room_type']; ?></h4>
                <h5><?php echo $row['room_rate']; ?></h5>
                <img src='../images/<?php echo $row['room_type']; ?>.jpg' width="250" height="160">
                <h5>Number of Adult&nbsp;
                    <select name='adult[]'> <!-- NOTICE HERE... -->
                        <?php
                        $x = 0;
                        while($x <= $row['max_cap']){
                            ?>
                        <option><?php echo $x; ?></option>
                            <?php
                            $x++;
                        }
                        ?>
                    </select>
                    &nbsp;&nbsp;&nbsp;Number of Children&nbsp;
    
                    <select name='children[]'>  <!-- NOTICE HERE... -->
                        <option>0</option>
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                    </select>
                </h5>
                <table class='table'>
                    <thead>
                        <th>Inclusive</th>
                        <th>Description</th>
                    </thead>
                    <tbody>
                        <td><?php echo $row['inclusive']; ?></td>
                        <td><?php echo $row['description']; ?></td>
                    </tbody>
                    <tfoot>
                        <td colspan='2'><?php echo $row['room_status']; ?></td>
                    </tfoot>
                </table>
            </div>
        </div>
            <?php
        }
        ?>
    </form>
    

    In the above code you will see that we are looping through some DB Rows. So there will be multiple Adult and Children selects. So for that case we are making them arrays with [] at the end. Which will preserve all inputs with same name inside an array.

    /**
     * After form submittion look at $_POST variable... eg.
     *
     * echo "<pre>";
     * print_r($_POST);
     * echo "</pre>";
     */
    
    /**
     * 
     * The $_POST['adult'] and $_POST['children'] will be an array. 
     * And then you can have whichever value you want by calling the right Index.
     *
     * Otherwise you can loop through them:
     * foreach ($_POST['adult'] as $key => $value) {
     *      echo $value;
     * }
     * And ---- for children.
     * foreach ($_POST['children'] as $key => $value) {
     *      echo $value;
     * }
     * 
     */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题