简体   繁体   中英

selected value get from db into Data list option using php mysql

I need to get selected value from db into datalist box.Tell me how to do it. Here is the code.

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

If i understood right , you need to select value in dropdownlist with other value also. You can achieve this by doing this

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

In above code, this line <?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?> <?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?> check if value are same from first query ,and if same then that option will be get selected automatically

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

try this code. im using echo <option> with while loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM