1

我需要从数据库中获取选定的值到数据列表框中。告诉我怎么做。这是代码。

<input list="Rank_Name" class="form-control" required>
                            <datalist id="Rank_Name">
                         <?php
                            $sel_cus = "select Rank_Name from ranks where Rank_Status=1";
                            $res_cus = mysqli_query($connection, $sel_cus);
                            while ($row = mysqli_fetch_array($res_cus)) {
                            ?>
                            <option value="<?php echo $row['Rank_Name'];?>"></option>
                           <?php
                            } 
                            ?>
                            </datalist>                   
4

2 回答 2

1

如果我理解正确,您还需要在下拉列表中选择其他值。您可以通过这样做来实现这一点

    <?php
    $select1="select Rank_Name from ranks where Rank_Status=1"; 
    $q=mysqli_query($select1) or die($select1);
    $row=mysqli_fetch_array($q); //here you are getting name of person whose rank is 1
    ?>

    <datalist id="Rank_Name">
     <?php 
            $s="select * from ranks ";
            $q=mysqli_query($s) or die($s);
            while($r=mysqli_fetch_array($q))
            { ?>
            <option value="<?php echo $r['Rank_Name']; ?>"<?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?>>
<?php echo $r['Rank_Name']; ?>
      </option>
            <?php } ?>
            </datalist>  

在上面的代码中,这一行<?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?>检查第一次查询的值是否相同,如果相同,则该选项将selected自动获取

于 2019-02-20T05:44:05.333 回答
0
<input list="Rank_Name" class="form-control" required>
                            <datalist id="Rank_Name">
                         <?php
                            $sel_cus = "select Rank_Name from ranks where Rank_Status=1";
                            $res_cus = mysqli_query($connection, $sel_cus);
                            while ($row = mysqli_fetch_array($res_cus)) {

                           echo "<option value=".$row['Rank_Name']."></option>";

                            } 
                            ?>
                            </datalist> 

试试这个代码。<option>我在 while 循环 中使用 echo

于 2019-02-20T05:55:17.970 回答