2013-08-21 81 views
-1

我試圖在多個下拉列表中的編輯頁面上預先選擇值,因此一旦用戶單擊編輯,就可以看到已插入的值。值被保存爲逗號分隔在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> 
+1

請之前發佈一個,因爲有很多重複的搜索類似於該問題(或者完全一樣)你的。 –

+1

您可能也有興趣閱讀:[如何獲得有用的錯誤消息在PHP?](http://stackoverflow.com/q/845021/367456) – hakre

+0

是很多重複,但找​​不到它們,爲什麼投票下來? – user974435

回答

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

作品令人驚歎:)做得很好 – user974435

0

你可以使用這樣的事情:

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

如果我理解正確的話,T如果所選值存儲在逗號分隔的字符串中,並且數字是之前選擇的值。

在這種情況下,答案很簡單:

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

錯誤在第9行 – user974435

+0

糟糕,固定它:) – Dragony