0

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

2
  • Does the content from the pick list come from two different sources? Are $q and $q2 just revisions of the same code? Where does the selected value come from, is it user input? Commented Dec 6, 2020 at 22:50
  • Your code appears to make rather little sense to begin with - you output a couple of <option>…</option> in a loop first, and then directly after that comes a <select>? Commented Dec 7, 2020 at 9:32

3 Answers 3

1

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

Comments

0

If you don't want to fetch same options again from the database you could use an SELECT DISTINCT statement in your select query.

And also handle the situation where the option is not same with the default one you chose.

while($row2 = mysqli_fetch_array($q2)) { if($row2[1] != $selected) $options = $options."<option>$row2[1]</option>"; } 

Comments

0
<select > <option value="1" <?php if ($Defaultselection == 1) echo "selected"; ?>>A</option> <option value="2" <?php if ($Defaultselection == 2) echo "selected"; ?>>B</option> <option value="3" <?php if ($Defaultselection == 3) echo "selected"; ?>>C</option> <option value="4" <?php if ($Defaultselection == 4) echo "selected"; ?>>D</option> </select> 

Try it

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.