从循环中取出正确的价值

时间:2016-01-03 07:40:54

标签: php mysqli

下方,您可以看到我的代码。无需显示其他部分或数据库表。

代码

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

我需要从正确的输入中获取值。此代码始终从循环中获取最后一个输入。我一直在努力研究这一天。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

由于输入的名称是重复的。尝试将输入的名称更改为Array。 例如:

<select name='children[]'></select>

答案 1 :(得分:0)

请按照以下代码:     

$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>

在上面的代码中,您将看到我们正在循环一些DB Rows。因此会有多个成人和儿童选择。因此,对于这种情况,我们最后使用[]制作数组。这将保留数组中具有相同名称的所有输入。

/**
 * 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;
 * }
 * 
 */