Solution 1 :

Have a look at the below options:

Option 1: Just added DISTINCT in SQL query

<select class="form-control" id="space" name="space">
   <option value="--Select--">--Select--</option>
   <?php
    $select=mysqli_query($con,"select DISTINCT `Space` from clients");
    while($menuz=mysqli_fetch_array($select)){
    $filled =$menuz['Space'];
    $valuez = array("C101","C102","C103","C104","C105","C106","C107","C108","W1","W2","W3","W4","W5","W6","W7","W8","W9","W10","W11","W12","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10");
    foreach($valuez as $value){
      if($value != $filled){
        ?>
            <option value="<?php echo $value;?>">
              <?php echo $value; ?>
            </option>
        <?php 
      }
    }
  }
  ?> 
</select>

OR

Option 2: Ignoring existing set of values from SQL query only using “NOT IN”. Here you can use prepared statement to avoid SQL injection. For demo purpose only, I had shown here a query with embedded input parameters.

<?php
  $valuez = array("C101","C102","C103","C104","C105","C106","C107","C108","W1","W2","W3","W4","W5","W6","W7","W8","W9","W10","W11","W12","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10");
?>
<select class="form-control" id="space" name="space">
   <option value="--Select--">--Select--</option>
   <?php
    $select=mysqli_query($con,"SELECT DISTINCT `Space` FROM `clients` WHERE `Space` NOT IN ('". implode("', '", $valuez) ."')");
    while($menuz=mysqli_fetch_array($select)){
      $filled =$menuz['Space'];
      ?>
      <option value="<?php echo $filled;?>">
        <?php echo $filled; ?>
      </option>
      <?php
    }
  ?> 
</select>

Problem :

I have a select dropdown in php which comes from a database. If the value is already present in the column it should not display in select dropdown, so I have written the following code:

<select class="form-control" id="space" name="space">
   <option value="--Select--">--Select--</option>
 <?php
  $select=mysqli_query($con,"select * from clients");
 while($menuz=mysqli_fetch_array($select))
  {
  $filled =$menuz['Space'];
 $valuez = array("C101","C102","C103","C104","C105","C106","C107","C108","W1","W2","W3","W4","W5","W6","W7","W8","W9","W10","W11","W12","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10");
 foreach($valuez as $value){
  if($value != $filled){ 





    ?>
        <option value="<?php echo $value;?>">
          <?php echo $value; ?>
        </option>
    <?php 
    } 
 }
}
?> 
</select>

now the problem is the values are displaying two times in the select column. first time its displaying all the values and after that its displaying values which are not in database as i want. can anyone please help me with this.

Comments

Comment posted by Niklesh Raut

Dont’ remove element from the dropdown, just make it auto selected.

Comment posted by Seep Sooo

@RishiRaut i didnt understand

Comment posted by Niklesh Raut

Make an array from database first and then use array function to intersect from 1st array to second array.

Comment posted by Seep Sooo

@RishiRaut i am not familiar with this, can you please show as an answer

Comment posted by array_diff()

array_diff()

By