pre-selected values for a dynamic drop-down box wi

2019-06-14 17:00发布

I am trying to pre select values on my edit page in multiple drop down so once user click edit, can see already inserted values. Values are saved as comma separated in MySQL like "1,2,3,4,5"

trying this solution but doesn't work :( , is there is any way that those vales will be pre selected? Please help

<select name="w_owning_branches[]" size="10" id="w_owning_branches" multiple="multiple" required>
<option value="" class="dropdown-option">  Select Owning Branch  </option>
<?php do {

$value = $row_branches['branch_id'];
$name = $row_branches['name'];
$selected = '1,2,3,4,5,6';

echo "<option value='$value'".(($selected == '$value') ? " selected='selected'":"").">$name</option>";


} while ($row_branches = mysql_fetch_assoc($branches)); ?>
</select>

3条回答
倾城 Initia
2楼-- · 2019-06-14 17:26

If I understand you correctly, then the selected values are stored in the comma separated string and the numbers are the values the where previously selected.

In that case the answer is simple:

<select name="w_owning_branches[]" size="10" id="w_owning_branches" multiple="multiple" required>
<option value="" class="dropdown-option">  Select Owning Branch  </option>
<?php do {

$value = $row_branches['branch_id'];
$name = $row_branches['name'];
$selected = '1,2,3,4,5,6';

echo "<option value='$value'".(in_array($value, explode(",",$selected)) ? " selected='selected'":"").">$name</option>";


} while ($row_branches = mysql_fetch_assoc($branches)); ?>
</select>
查看更多
你好瞎i
3楼-- · 2019-06-14 17:31

try:

<select name="w_owning_branches[]" size="10" id="w_owning_branches" multiple="multiple" required>
<option value="" class="dropdown-option">  Select Owning Branch  </option>
<?php do {

$value = $row_branches['branch_id'];
$name = $row_branches['name'];
$selected = '1,2,3,4,5,6';
$selected_values = explode(",",$selected);

echo "<option value='$value'".((in_array($value,$selected_values)) ? " selected='selected'":"").">$name</option>";


} while ($row_branches = mysql_fetch_assoc($branches)); ?>
</select>
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-06-14 17:32

You could use something like this:

$selected = '1,2,3,4,5,6';
$selectedArr = explode(",", $selected);
echo "<option value='$value'".((in_array($value, $selectedArr))?" SELECTED ":"").">$name</option>";
查看更多
登录 后发表回答