douju4278 2015-02-01 16:47
浏览 97
已采纳

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

I have an HTML form that a user can use to edit a row in a mysql database, the PHP script queries the database and then displays the current data in form. I have this working fine for text fields with something like:

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

and simple radio set up like:

<?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 } ?>

I also have a select option element like below:

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>

Which im trying to set up, I could use:

<?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 } ?>

But perhaps there is a better method to do this without duplicating so much code?

  • 写回答

2条回答 默认 最新

  • dongwu9170 2015-02-01 17:44
    关注

    You can add an if clause on your option creation that will at the selected attribute if needed.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度