2015-02-10 181 views
0

我要動態地選擇在下拉菜單 值默認值存儲是由$row['order status'] 檢索MySQL服務器上,它只有4個存儲在服務器值Open Pending Closed Success如何選擇默認值下拉菜單

所以我必須從這些值4

$selected are all initialised to false or '0' or disabled 

我試圖下面的代碼選擇默認值,但它不工作

if($row["Order Status"]=="Open"){ 
     $selected='selected'; 
    } 
    if($row["Order Status"]=="Pending"){ 
     $selected1='selected'; 
    } 
    if($row["Order Status"]=="Closed"){ 
     $selected2='selected'; 
    } 
    if($row["Order Status"]=="Success"){ 
     $selected3='selected'; 
    } 
    echo" 
    <select id='txt_orderstatusid_' class='txt_id_' > 
     <option ".$selected." value='Open'>Open </option> 
     <option ".$selected1." value='Pending'>Pending </option> 
     <option ".$selected2."value='Closed'>Closed </option> 
     <option ".$selected3."value='Success'>Success </option> 
    </select>" 

我已經試過這也

echo "<select id='txt_orderstatusid_' class='txt_id_' > 
<option selected=".$selected." value='Open'>Open </option> 
<option selected=".$selected1." value='Pending'>Pending </option> 
<option selected=".$selected2."value='Closed'>Closed </option> 
<option selected=".$selected3."value='Success'>Success </option> 
</select> " 

它也不起作用

回答

4

您可以在選擇檢查分貝值直接嘗試

<select id='txt_orderstatusid_' class='txt_id_' > 
    <option <?php if(isset($row["Order Status"]) && $row["Order Status"]=="Open") echo 'selected="selected"';?> value='Open'>Open </option> 
    <option <?php if(isset($row["Order Status"]) && $row["Order Status"]=="Pending") echo 'selected="selected"';?> value='Pending'>Pending </option> 
    <option <?php if(isset($row["Order Status"]) && $row["Order Status"]=="Closed") echo 'selected="selected"';?> value='Closed'>Closed </option> 
    <option <?php if(isset($row["Order Status"]) && $row["Order Status"]=="Success") echo 'selected="selected"';?> value='Success'>Success </option> 
</select> 
+0

你能應歸結爲你做了什麼,我在這裏的代碼無法理解。 – vaibhavcool20 2015-02-10 06:05:41

+0

只是將db值與選項值匹配並添加選中的 – 2015-02-10 06:20:27

+0

它不起作用,因爲我的整個代碼都在echo中。 – vaibhavcool20 2015-02-10 06:33:50