2

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

3 Answers 3

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

i want to display $valuez , if values in $valuez are not present in $filled
@SeepSooo Yes, you don't need to use array, that will slow down your query performance
still nothing bro
1

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

6 Comments

i have used your code, still not displaying the values
you are echoing array here use a single value instead . SO I guess you need to use $filled here <option value="<?php echo $filled;?>"><?php echo $filled;?> updated the code as well
i want to display $valuez , if values in $valuez are not present in $filled
okay then you need to use foreach and iterate through values array
can you please update the code and show, it would be great
|
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.