2017-04-04 110 views
0

當我嘗試從下拉列表中選擇SQL選擇的選項,我得到的所有選項中選擇:PHP和MySQL的下拉選項中選擇問題

$status1 = $row7["status"]; 

if($status1 == "draft") { $slc = "selected"; } 
if($status1 == "ordered") { $slc = "selected"; } 
if($status1 == "shipped") { $slc = "selected"; } 

echo " 
    <select class=\"form-control input-sm\" name = \"o_status\"> 
    <option value = \"draft\" $slc>draft</option> 
    <option value = \"ordered\" $slc>ordered</option> 
    <option value = \"shipped\" $slc>shipped</option> 
    </select> 
"; 

我與輸出的問題選擇了所有選項那樣:

<select class="form-control input-sm" name = "status"> 
<option value = "draft" selected>draft</option> 
<option value = "ordered" selected>shipped</option> 
<option value = "shipped" selected>shipped</option> 

回答

3

請立即檢查。

希望這有助於你:

$status1 = $row7["status"]; 

if($status1 == "draft") { $draft = "selected"; } 
if($status1 == "ordered") { $ordered = "selected"; } 
if($status1 == "shipped") { $shipped = "selected"; } 

    echo " 
    <select class=\"form-control input-sm\" name = \"o_status\"> 
<option value = \"draft\" $draft>draft</option> 
<option value = \"ordered\" $ordered>ordered</option> 
<option value = \"shipped\" $shipped>shipped</option> 
</select> 
    "; 
+1

這將導致錯誤,如 PHP注意:未定義的變量:在第22行的thisfile.php中排序 當選擇其中一個時,其他未定義。 看看我的回覆;) –

+0

Thx Ajay現在工作很好:) – Sky

+0

歡迎:) – Aaron

4

您可以以設置選擇選項定義狀態的數組:

<?php 
    $statuses = array(
     'draft' => 'New draft', 
     'ordered' => 'New order', 
     'shipped' => 'New ship' 
    ); 
?> 

<select class="form-control input-sm" name="status"> 
<?php 
    foreach ($statuses as $status => $name) { 
?> 
    <option value="<?php echo $status ?>" <?php echo $status === $row7['status'] ? 'selected="selected"' : '' ?>> 
     <?php echo $name ?> 
    </option> 
<?php 
    } 
?> 
</select> 
+0

如果選項名稱不同的期權價值如何設置呢? – Sky

+0

Like:等等。 – Sky

+0

是的,您可以: 試試這個: '<?php $ statuses = array('draft'=>'something1','ordered'=> 'something2','shipped'=>'something3'); $ row7 ['status'] ='草稿'; ?> <選擇類= 「形式控制輸入-SM」 名稱= 「狀態」> <?PHP的 的foreach($狀態爲$狀態=> $值){ ?> <選項值=」 <?php echo $ value?>「<?php echo $ status === $ row7 ['status']? '選擇= 「選擇」': '' >> <?PHP的echo $狀態> ' –

1

也許這會有所幫助:

<?php 
switch ($row7["status"]) { 
    case 'draft': 
     $draft = "selected"; 
     $shipped = ""; 
     $ordered = ""; 
     break; 
    case 'ordered': 
     $ordered = "selected"; 
     $draft = ""; 
     $shipped = ""; 
     break; 
    case 'shipped': 
     $shipped = "selected"; 
     $draft = ""; 
     $ordered = ""; 
     break; 
    default: 
     $draft = ""; 
     $shipped = ""; 
     $ordered = ""; 
     break; 
} 
echo "<select class=\"form-control input-sm\" name = \"o_status\"> 
    <option value = \"draft\" $draft>draft</option> 
    <option value = \"ordered\" $ordered>ordered</option> 
    <option value = \"shipped\" $shipped>shipped</option> 
    </select>"; 
?>