简体   繁体   中英

displaying values from sql in html select option not displaying values

i have created a html form using php and sql. The form contains a field called spaces which is a select dropdown. The values are coming from an array which i declared in php. If the values in array are already present in database it should not display. So i have done the following code:

 <select class="form-control" id="space" name="space"> <option value="--Select--">--Select--</option> <?php $select=mysqli_query($con,"select * from clients where Spaces IS NOT NULL"); while($menu1=mysqli_fetch_array($select)) { $filled =$menu1['name']; $valuez = array("C101","C102","C103","C104"); if ($filled != $valuez) { ?> <option value="<?php echo $valuez;?>"> <?php echo $valuez;?> </option> <?php }} ?> </select>

but this is not making any values display. Can anyone please tell me what is wrong in my code. thanks n advance

Your $filled is string, cannot equals to array.

And you can just use whereIn , don't need to take all clients out:

<select class="form-control" id="space" name="space">
  <option value="--Select--">--Select--</option>
  <?php
    $valuez = "'C101','C102','C103','C104'";
    $select = mysqli_query($con, "SELECT * FROM clients WHERE Space IN ($valuez)");
  ?>

<?php while($menu1 = mysqli_fetch_array($select) ) : ?>
    <option value="<?php echo $menu1['name'];?>">
      <?php echo $menu1['name'];?>
    </option>
<?php endwhile; ?> 
</select>

you are comparing a string with the array. you should use in_array like this

 <select class="form-control" id="space" name="space">
   <option value="--Select--">--Select--</option>
 <?php
  $select=mysqli_query($con,"select * from clients where Space IS NOT NULL");
 while($menu1=mysqli_fetch_array($select))
  {
  $filled =$menu1['Space'];
 $valuez = array("C101","C102","C103","C104");
 foreach($valuez as $value){
    if($value != $filled){ 
    ?>
        <option value="<?php echo $value;?>">
          <?php echo $value;?>
        </option>
    <?php 
    }
 }
}
?>

update the code

Try this and please make sure the value you want to display as the options are from the column name or spaces. I used the name($menu1['name']) column as per your code.

<select class="form-control" id="space" name="space">
  <option value="--Select--">--Select--</option>
   <?php
     $select = mysqli_query($con,"select * from clients where Spaces NOT IN ('C101','C102','C103','C104')");
     while($menu1=mysqli_fetch_array($select)) {
       $filled = $menu1['name'];
       if (!empty($filled)) {
   ?>
   <option value="<?php echo $filled;?>">
     <?php echo $filled;?>
   </option>
  <?php
    }}
  ?>
</select>

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