Loop整个选择Tag

时间:2016-03-11 10:49:27

标签: php html mysqli drop-down-menu

是否可以循环整个选择标记以具有多个下拉菜单? 我想要实现的是在表中添加一个下拉菜单列。

这是我到目前为止所尝试的

<?php
    $DNS_FROM = $DNS."_port-%";
    $select = "SELECT * FROM `uplink_port_mapping` WHERE DNS_From LIKE '$DNS_FROM'";
    $select1 = mysqli_query($conn, $select);
    $select2 = "SELECT DNS_From FROM `uplink_port_mapping` WHERE DNS_From NOT LIKE '$DNS_FROM' AND DNS_To = ''";
    $select3 = mysqli_query($conn, $select2);
    while($uplink_from = mysqli_fetch_assoc($select1)){
        echo "<tr>";
        echo "<td>".$uplink_from['DNS_From']."</td>";
        echo "<td>"."<select name = 'uplink_to' multiple='multiple'> 
        <option value = '".$uplink_from['DNS_To']."' selected='selected'>". $uplink_from['DNS_To']."</option>";

        while ($uplink_to = mysqli_fetch_assoc($select3)){
                echo "<option value='".$uplink_to['DNS_From']."'>".$uplink_to['DNS_From']."</option>";
        }

        echo"</select>";
        echo"</td>";
        echo"</tr>";
    }
?>

How it is right now.

2 个答案:

答案 0 :(得分:1)

第二个while循环只运行一次的原因是mysqli_fetch_assoc($result)将$ result recource的指针留在它的结尾。 因此,当您尝试第二次循环时,mysqli_fetch_assoc($result)将不会返回任何内容(因为它在$ result recource的末尾。

两个可能性:

将指针重置为开头:

<?php 
....
while($uplink_from = mysqli_fetch_assoc($select1)){
    echo "<tr>";
    echo "<td>".$uplink_from['DNS_From']."</td>";
    echo "<td>"."<select name = 'uplink_to' multiple='multiple'> 
    <option value = '".$uplink_from['DNS_To']."' selected='selected'>". $uplink_from['DNS_To']."</option>";

    // here's the change:
    mysql_data_seek($select3, 0);

    while ($uplink_to = mysqli_fetch_assoc($select3)){
            echo "<option value='".$uplink_to['DNS_From']."'>".$uplink_to['DNS_From']."</option>";
    }

    echo"</select>";
    echo"</td>";
    echo"</tr>";
}
.... 
?>

或者 - 我认为是更好的解决方案 - 首先将数据存储到数组中,然后遍历该数组:

<?php
...
$select2 = "SELECT DNS_From FROM `uplink_port_mapping` WHERE DNS_From NOT LIKE '$DNS_FROM' AND DNS_To = ''";
$result2 = mysqli_query($conn, $select2);
$dns_from = Array();
while ($uplink_to = mysqli_fetch_assoc($select3)){
    $dns_from[] = $uplink_to;
}

....
// inside your first while loop:
foreach($dns_from as $dns) {
   echo "<option value='".$dns['DNS_From']."'>".$dns['DNS_From']."</option>";
}
....
// note that I left out a bunch of your code, that doesn't change.
?>

答案 1 :(得分:0)

多个选择下拉基本示例

.img-responsive{
    min-height: 300px; // Adjust the height of your min height
    background-image: url('background.jpg;');
    background-repeat: no-repeat;
    background-position: center center;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}