简体   繁体   English

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

[英]Pre-select option matching db value without adding a new Select option

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. 我的问题特定于Select元素 - 而不是从现有的选择选项中进行选择,而是在数据库中添加一个带有值的额外选择选项,以便数据库中的值显示两次。

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* 编辑 -例如,如果'gift_privacy'的db值为'Standard',则Select选项显示为:* Standard,Standard,Gift ID Req,Not_Enrolled *而不是* Standard,Gift ID Req,Not_Enrolled *

How can I correct this code so that selected="selected" is applied to the existing options? 如何更正此代码,以便将selected="selected"应用于现有选项?

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

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. 当然,有多种方法可以对猫进行换肤,您可以检查$rows['gift_privacy']与每行的当前选项匹配。

Try this . 尝试这个 。 This is simple solution for your problem. 这是解决您问题的简单方法。

<select name="gift_privacy">
<option value="Standard" <?php if($rows['gift_privacy']=='Standard') echo "selected='selected'"; ?>>Standard</option>
<option value="Gift_ID_Req" <?php if($rows['gift_privacy']=='Gift_ID_Req') echo "selected='selected'"; ?>>Standard</option>
<option value="Not_Enrolled" <?php if($rows['gift_privacy']=='Not_Enrolled') echo "selected='selected'"; ?>>Standard</option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM