簡體   English   中英

顯示從mysql到php html中的dropdownbox的特定信息。

[英]Show specific information from mysql to dropdownbox in php html.

這些是我的代碼,用於從數據庫中獲取信息。 因此狀態是我的數據庫中感興趣或不感興趣的狀態。

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

首先-在選擇中不需要逗號,其次-確保這是唯一具有狀態ID的元素,其次-只需檢查每個選項中的$ status值,然后選擇是否回顯。

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

改變這個

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;

如果值與文本相同,則不需要value屬性。

如果有兩個以上的選擇,我建議這樣的循環:

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

因為默認是第一個選項Interested ,那么if ($status === "Not Interested")設置option屬性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>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM