根据 mysql 字段设置所选选项


Set selected option based on mysql field

我有一个HTML表单,用户可以使用它来编辑mysql数据库中的行,PHP脚本查询数据库,然后以表单显示当前数据。我对于具有以下内容的文本字段工作正常:

Correct Answer:<input type="text" name="answerswer" value="<?php echo $row['CorrectAnswer']; ?>"><br>

和简单的收音机设置,例如:

<?php 
            if ($row['Hazardous'] == "no"){
        ?>      
        Hazardous?:<input type="radio" name="hazardous" value="yes">Yes 
            <input type="radio" name="hazardous" value="no" checked="checked">No<br>
        <?php
            }else{
        ?>
        Hazardous?:<input type="radio" name="hazardous" value="yes" checked="checked">Yes   
            <input type="radio" name="hazardous" value="no" >No<br>
        <?php } ?>

我还有一个选择选项元素,如下所示:

Category: <select name="category">
                <option value="hazardawareness">Hazard Awareness</option>
                    <option value="observation">Observation</option>
                    <option value="insurance">Insurance</option>
                    <option value="attitude">Attitude</option>
                    <option value="knowledge">Gen. Knowledge</option>
            </select><br>

尝试设置哪个,我可以使用:

<?php if ($row['Category'] == "hazardawareness"){ ?>        
    Category: <select name="category">
                <option value="hazardawareness" selected="selected">Hazard Awareness</option>
                    <option value="observation">Observation</option>
                    <option value="insurance">Insurance</option>
                    <option value="attitude">Attitude</option>
                    <option value="knowledge">Gen. Knowledge</option>
            </select><br>   
<?php }else if ($row['Category'] == "observation"){ ?>
Category: <select name="category">
                <option value="hazardawareness">Hazard Awareness</option>
                    <option value="observation" selected="selected">Observation</option>
                    <option value="insurance">Insurance</option>
                    <option value="attitude">Attitude</option>
                    <option value="knowledge">Gen. Knowledge</option>
            </select><br>   
<?php }else if ($row['Category'] == "insurance"){ ?>
Category: <select name="category">
                <option value="hazardawareness">Hazard Awareness</option>
                    <option value="observation">Observation</option>
                    <option value="insurance"selected="selected">Insurance</option>
                    <option value="attitude">Attitude</option>
                    <option value="knowledge">Gen. Knowledge</option>
            </select><br>   
<?php }else if ($row['Category'] == "attitude"){ ?>
Category: <select name="category">
                <option value="hazardawareness">Hazard Awareness</option>
                    <option value="observation">Observation</option>
                    <option value="insurance">Insurance</option>
                    <option value="attitude" selected="selected">Attitude</option>
                    <option value="knowledge">Gen. Knowledge</option>
            </select><br>   
<?php }else if ($row['Category'] == "knowledge"){ ?>
Category: <select name="category">
                <option value="hazardawareness" >Hazard Awareness</option>
                    <option value="observation">Observation</option>
                    <option value="insurance">Insurance</option>
                    <option value="attitude">Attitude</option>
                    <option value="knowledge" selected="selected">Gen. Knowledge</option>
            </select><br>   
<?php } ?>

但是也许有一种更好的方法可以在不重复太多代码的情况下做到这一点?

您可以在

option创建中添加一个if子句,如果需要,该子句将在selected属性中添加。

<?php $options = array( 
                 "Hazard Awareness" => hazardawareness, 
                 "Observation" => observation,
                 "Insurance" => insurance, 
                 "Attitude" => attitude 
              );  ?>
<select name="category">
<?php  foreach($options as $display => $value) {  ?>
    <option value='<?= $value ?>' <?php if($row['Category'] == trim($value)) { ?>selected='selected'<?php } ?>>
        <?= $display ?>
    </option>
<?php } ?>
</select>

我认为这将帮助您使用更少的代码来完成工作:

<?php
$options = array(
"Hazard Awareness" => hazardawareness,
"Observation" => observation,
"Insurance" => insurance,
"Attitude" => attitude
);
echo 'Category:  <select name="category">';
foreach($options as $display => $value) {
if ($row['Category'] == trim($value)) {
    echo '<option value="' . $value . '" selected>' . $display .'</option>';
}
else {
    echo '<option value="' . $value . '">' . $display . '</option>';
}
}
echo '</select>';

将您的期权数据放在array() 中。