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