php - Pre-select option matching db value without adding a new Select option -
i creating client business relationship page displays relevant info in database , allows customers edit information.
the code below retrieves , displays customer's business relationship info database , pre-selects values matching info correctly. problem specific select elements - instead of selecting amongst existing select options it’s adding select alternative value in database, value that’s in database displayed twice.
edit - example, if db value 'gift_privacy' 'standard', select options beingness displayed as: *standard, standard, gift id req, not_enrolled* instead of *standard, gift id req, not_enrolled*
how can right code selected="selected"
applied existing options?
<?php seek { $stmt = $conn->prepare("select * customer_info 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 programme id</option> <option value="not_enrolled">do not enroll</option> </select>
try:
<?php $gifts = array( "standard" => "standard", "gift_id_req" => "require programme id", "not_enrolled" => "do not enroll"); $gift = $rows['gift_privacy']; //$gift = "gift_id_req"; foreach($gifts $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 programme id</option> <option value="not_enrolled">do not enroll</option>
of course, there multiple ways skin cat, , can check see if $rows['gift_privacy']
matches current alternative on each line instead.
php select selected
No comments:
Post a Comment