我正在尝试更新html表单中的user_pref
列。
我有一个看起来像这样的下拉(它工作正常):
User pref: <select name="user_pref" size="1" id="user_pref">
<option value="BLUE">Blue</option>
<option value="RED">Red</option>
<option value="YELLOW">Yellow</option>
<?php echo htmlentities($user_pref, ENT_COMPAT, 'utf-8'); ?></select>
我希望能够将默认值设置为用户在MySQL的user_pref
列中已有的值。因此,例如,如果用户A的user_pref中有"Yellow"
,那么我想在更新该用户的偏好时显示"Yellow"
。
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
这是基本的想法......你必须根据自己的需要进行调整......
<?php
// assuming $user_pref is their preference...
/*
* create array of possible options...
* I usually store my options in my database because
* my applications tend to be dynamic
* but you can just create a quick array to do the same job
*/
$arrOptions = array(
'val1' => 'option1',
'val2' => 'option2',
'val3' => 'option3'
);
$selected = '';
print '<select id="user_pref_id">';
foreach ($arrOptions as $key => $val) {
// test for $key or $val depending on which makes sense for your application
$selected = ($key == $user_pref) ? ' selected="selected"' : '';
printf('<option value="%s"%s>%s</option>', $key, $selected, $val);
}
print '</select>';
?>