根据mysql字段设置选定的选项

时间:2015-02-01 16:47:26

标签: php mysql

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

Correct Answer:<input type="text" name="answer" 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 } ?>

但也许有一种更好的方法可以做到这一点而不需要复制如此多的代码?

2 个答案:

答案 0 :(得分:1)

如果需要,您可以在if创建option创建的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> 条款。

{{1}}

答案 1 :(得分:0)

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

<?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()