doujiao3998 2013-02-12 03:33
浏览 47
已采纳

预选选项匹配db值而不添加新的Select选项

I am creating a customer account page which displays the relevant information in the database and allows customers to edit their information.

The code below retrieves and displays the customer's account information from the database and pre-selects the values matching their information correctly. My problem is specific to Select elements - instead of selecting from amongst the existing select options it’s adding an extra select option with the value in the database, so that the value that’s in the database is displayed twice.

Edit - For example, if the db value for 'gift_privacy' is 'Standard', the Select options are being displayed as: *Standard, Standard, Gift ID Req, Not_Enrolled* instead of *Standard, Gift ID Req, Not_Enrolled*

How can I correct this code so that selected="selected" is applied to the existing options?

<?php   
try {  
$stmt = $conn->prepare("SELECT * FROM customer_info WHERE user_id = :user_id");  
$stmt->bindValue(':user_id', $user_id); 
$stmt->execute();
}catch(PDOException $e) {echo $e->getMessage();}
$row = $stmt->fetch();
$search = array('_', ',');
$replace = array(' ', ', ');
$rows = str_replace($search, $replace, $row);
?>
<select name="gift_privacy">
<option selected="selected" value="<?php echo $rows['gift_privacy']; ?>"><?php echo $rows['gift_privacy']; ?></option>
<option value="Standard">Standard</option>
<option value="Gift_ID_Req">Require program ID</option>
<option value="Not_Enrolled">Do not enroll</option>
</select>
  • 写回答

2条回答 默认 最新

  • duanniubeng2265 2013-02-12 04:14
    关注

    Try:

    <?php
    $gifts = array(
        "Standard" => "Standard",
        "Gift_ID_Req" => "Require Program ID",
        "Not_Enrolled" => "Do not Enroll");
    
    $gift = $rows['gift_privacy'];
    //$gift = "Gift_ID_Req";
    foreach($gifts as $key => $value) {
        if($key == $gift) {
            echo '<option selected="selected" value="'. $key .'">'. $value .'</option>';
        } else {
            echo '<option value="'. $key .'">'. $value .'</option>';
        }
    }
    ?>
    

    instead of:

    <option selected="selected" value="<?php echo $rows['gift_privacy']; ?>"><?php echo $rows['gift_privacy']; ?></option>
    <option value="Standard">Standard</option>
    <option value="Gift_ID_Req">Require program ID</option>
    <option value="Not_Enrolled">Do not enroll</option>
    

    Of course, there are multiple ways to skin a cat, and you can check to see if $rows['gift_privacy'] matches the current option on each line instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)