具有多个值的动态下拉框的预选值

时间:2013-08-21 17:42:10

标签: php mysql drop-down-menu

我正在尝试在多个下拉列表中预先选择我的编辑页面上的值,因此一旦用户单击编辑,就可以看到已插入的值。值在“MySQL”中以逗号分隔保存,如“1,2,3,4,5”

尝试此解决方案但不起作用:(,是否有任何方式可以预先选择这些值?请帮助

<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 个答案:

答案 0 :(得分:1)

如果我理解正确,那么所选的值将存储在逗号分隔的字符串中,而数字则是先前选择的值。

在这种情况下答案很简单:

<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>

答案 1 :(得分:0)

您可以使用以下内容:

$selected = '1,2,3,4,5,6';
$selectedArr = explode(",", $selected);
echo "<option value='$value'".((in_array($value, $selectedArr))?" SELECTED ":"").">$name</option>";

答案 2 :(得分:0)

尝试:

<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>