2016-06-12 47 views
0

這些是我從數據庫中獲取信息的代碼。所以狀態是我的數據庫中有興趣或不興趣的地方。在php html中顯示從mysql到dropdownbox的具體信息。

while($row= mysqli_fetch_array($result)) 
{ 
    $ID =$row['Particulars_ID']; 
    $name = $row['Name']; 
    $number =$row['Number']; 
    $status =$row['Status']; 
    $remarks =$row['Remarks']; 
} 

如果我的數據庫顯示此人不感興趣,我想回顯出不感興趣的值。然而,從我的下面的代碼,它總是顯示感興趣,無論我點擊哪個人。

echo "<select name = 'status', id = status> 
     <option value='Interested'>Interested</option> 
     <option value='Not Interested'>Not Interested</option> 
    </select><br>"; 

回答

1

第一 - 你不需要逗號的選擇,第二 - 確保這是與身份標識的唯一元素,第三 - 只需在每個選項檢查$狀態值,如果回聲選擇是。

echo "<select name = 'status' id = 'status'> 
     <option value='Interested'"; 
      if($status == "Interested"){echo " selected";} 
     echo">Interested</option> 
     <option value='Not Interested' "; 
      if($status == "Not Interested"){echo " selected";} 
     echo">Not Interested</option> 
    </select><br>"; 
0

更改此

echo "<select name = 'status', id = status> 
     <option value='Interested'>Interested</option> 
     <option value='Not Interested'>Not Interested</option> 
    </select><br>"; 

這個

// Set the selected attributes based on the value of status 
$interested = ($status === 'Interested') ? ' selected' : ''; 
$notInterested = ($interested === '') ? ' selected' : ''; 

echo <<< SELECT 
<select name="status" id="status"> 
    <option$interested>Interested</option> 
    <option$notInterested>Not Interested</option> 
</select> 
SELECT; 

如果值是一樣的文字你不需要屬性的值。

如果有兩個以上的選擇,我推薦一個循環是這樣的:

<?php foreach ($options as $o) : ?> 
    <option<?php if ($o === $optionValue) ?> selected<?php endif ?>><?= optionValue ?></option> 
<?php endforeach ?> 
0

因爲默認是第一個選項Interested,然後if ($status === "Not Interested")設置option attribute selected

<?php 

if ($status === "Not Interested") $selected = "selected"; 
else $selected = ""; 

echo "<select name = 'status', id = status> 
     <option value='Interested'>Interested</option> 
     <option value='Not Interested' $selected>Not Interested</option> 
    </select><br>";