There is a form in which there are elements, in each of which there are 2 selection points: , . I choose the items I need and send it all to the database.

enter image description here

But I also need to be able to edit this data. That is, open the edit form and pull data there from the database.

enter image description here

There are no problems with ordinary . But with , I got a snag. The problem is that when editing I cannot offer the same 2 options, that is, one - selected by adding (by default), and the other opposite to it.

I suspect that this is done with the help of the condition, but I cannot understand how exactly.

Started like this:

<? if ($select_pay) echo "<option selected>$select_pay</option>"; ?> 

How to display another option that was not selected when adding? The $ select_pay stores the selected option when adding, which I pulled out of the database.

The itself looks like this:

 <select size=1 name=pay> <option value="Включен">Включен</option> <option value="Выключен">Выключен</option> </select> 
  • If you always guarantee exactly two values ​​in selects, wouldn't it be more convenient to use checkboxes instead? - Yaant
  • I wanted it from the beginning, but I was told to do it on selects, I do not know why. Master-master. - luckydutch

1 answer 1

Create an array of all options ("on", "off", something else). In the foreach loop, foreach through all the options, and make selected the one that is stored in the database.

 $pays = array("on", "off"); foreach ($pays as $pay){ ?> <option value="<?php echo $pay; ?>" <?php if($pay == $select_pay) echo "selected"; ?>> <?php echo $pay; ?> </option> <?php } ?> } 
  • It works, thank you very much. - luckydutch