如何从循环中获取多个下拉值?

时间:2016-10-10 01:07:19

标签: php html mysql

<?php
    // this is where selecting of number of rooms in database
    $sql = mysqli_query($con, 'SELECT * FROM roomtype');
    $get_room1 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=2");
    $get_room2 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=3");
    $get_room3 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=4");
    $get_room4 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=5");
    $standard = mysqli_num_rows($get_room1);
    $deluxe = mysqli_num_rows($get_room2);
    $family = mysqli_num_rows($get_room3);
    $regular = mysqli_num_rows($get_room4);
    // this is the loop for the drop down and it will display the number of rooms available

    while($row = mysqli_fetch_array($sql))
    {
    $a=$row['type_id'];
    $b = $row['rmtype'];
    $_SESSION['room_type'] = $row['rmtype'];
    echo '<div style="height: 300px;">';
    echo '<div style="float: left; width: 100px; margin-top: 15px; margin-left: 60px;">';
    echo "<img width=250 height=200 alt='Unable to View' src='" . $row["image"] . "'>";
    echo '</div>';
    echo '<div class="well" style="float: right; width: 780px; margin-top: 5px;">';
                echo '<div style="float: right;">';
                echo '</div>';

    echo '<br />';
    echo "<label style='margin-left: px;'>Number of rooms:";
    echo "<select name='select_no_rooms[]' value='" .$row['rmtype']." ' />";
    echo "<option> </option>";
    if ($row['rmtype']=="Standard Room")
    {
        for ($x = 1; $x<=$standard; $x++){
            echo "<option name='standard'>$x</option>";}

    }
    if ($row['rmtype']=="Deluxe Room")
    {
        for ($x = 1; $x<=$deluxe; $x++){
            echo "<option name='deluxe'>$x</option>";}

    }
    if ($row['rmtype']=="Family Room")
    {
        for ($x = 1; $x<=$family; $x++){
            echo "<option name='family'>$x</option>";}

    }
    if ($row['rmtype']=="Regular Room")
    {
        for ($x = 1; $x<=$regular; $x++){
            echo "<option name='regular'>$x</option>";} 
    }
    echo "</select>";

    }
?> 

这是下拉设计,

enter image description here

2 个答案:

答案 0 :(得分:1)

[ ]标记添加到名称标记,这样您就可以在数组中获取值。

<option name='deluxe[]'>$x</option>

答案 1 :(得分:0)

我注意到的第一件事是,在select语句中,您不能将value作为属性。请查看文档select tag

option标记不包含name属性。请查看文档option tag

如果您想从下拉列表中获取多个值。请检查this article