Skip to content
Advertisement

Make select default option from database

i have issue with making default text in my select. (option list). So, my option is storage in database. Like varchar Test Option

Now if i make like this in my code

 <select  name="category">  <option  selected="selected"><?=$ad['category']?></option> <?php echo $options; ?> </select>

it working, but in my list be two same values then. my full code.

              <?php

                while($category = mysqli_fetch_array($q)):;

                 ?>

              <option> value="<?php echo $category[0];?>" <?php echo $category[1];?> </option>"

               <?php endwhile; ?>


                

               <select  name="category">  <option  selected="selected"><?=$ad['category']?></option> <?php echo $options; ?> </select>
                
 
<?php

My code for function q and pick value from database (all value from option is from database)

   $q2 = mysqli_query($con,$q22222);
    $options = "";
     while($row2 = mysqli_fetch_array($q2))
     {
         $options = $options."<option>$row2[1]</option>";
     }

picture what explain situation more. enter image description here

any idea how to solve it ? all help will be appreciated

Advertisement

Answer

Well, the real situation is that you are fetching an array but you want that the option won’t be printed. Then, you first have the selected option, that is:

<?php $ad['category']; ?>

Then the only thing that you have to do is that when you fetch the array, if the option is equal to the selected option won’t be printed so:

   $q2 = mysqli_query($con,$q22222);
    $options = "";
     while($row2 = mysqli_fetch_array($q2))
     {
         if($row2[1]!=$selected){
              $options = $options."<option>$row2[1]</option>";
         }
     }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement