我有下一个模板:
<select name="interest">
<option value="seo">SEO и Блоговодство</option>
<option value="auto">Авто</option>
<option value="business">Бизнес</option>
<option value="design">Дизайн</option>
...
并将结果值存储在$result['interest']
。
如何将option
元素标记为使用PHP选择?
谢谢!
答案 0 :(得分:29)
手动方式.....
<select name="interest">
<option value="seo"<?php if($result['interest'] == 'seo'): ?> selected="selected"<?php endif; ?>>SEO и Блоговодство</option>
.....
更好的方法是循环利益
$interests = array(
'seo' => 'SEO и Блоговодство',
'auto' => 'Авто',
....
);
<select name="interest">
<?php foreach( $interests as $var => $interest ): ?>
<option value="<?php echo $var ?>"<?php if( $var == $result['interest'] ): ?> selected="selected"<?php endif; ?>><?php echo $interest ?></option>
<?php endforeach; ?>
</select>
答案 1 :(得分:11)
<?php
$interests = array('seo' => 'SEO и Блоговодство', 'auto' => 'Aвто', 'business' => 'Бизнес', ...);
?>
<select name="interest">
<?php
foreach($interests as $k => $v) {
?>
<option value="<?php echo $k; ?>" <?php if($k == $result['interest']) ?> selected="selected" <?php } ?>><?php echo $v;?></option>
<?php
}
?>
</select>
答案 2 :(得分:4)
<select name="interest">
<option value="seo"<?php if($result['interest'] == 'seo'){ echo ' selected="selected"'; } ?>>SEO</option>
<option value="auto"<?php if($result['interest'] == 'auto'){ echo ' selected="selected"'; } ?>>Auto</option>
<option value="business"<?php if($result['interest'] == 'business'){ echo ' selected="selected"'; } ?>>Business</option>
<option value="design"<?php if($result['interest'] == 'design'){ echo ' selected="selected"'; } ?>>Design</option>
</select>
答案 3 :(得分:4)
<?php
$list='<select name="interest">
<option value="seo">SEO и Блоговодство</option>
<option value="auto">Авто</option>
<option value="business">Бизнес</option>
<option value="design">Дизайн</option>
...';
echo str_replace('value="' . $result['interest'] . '"','value="' . $result['interest'] . '" selected',$list);
&GT?; 这包括创建一个包含列表的字符串,然后使用字符串替换函数来查找正确的选项并将选定内容添加到标记中。如果使用XHTML,则需要使用selected =“selected”。
http://sandbox.onlinephpfunctions.com/code/37eb8f5a213fe5a252cd4da6712f3db0c5558ae3
答案 4 :(得分:1)
<select name="interest">
<option value="seo" <?php echo $result['interest'] == 'seo' ? 'selected' : ''?> >SEO и Блоговодство</option>
<option value="auto" <?php echo $result['interest'] == 'auto' ? 'selected' : ''?>>Авто</option>
<option value="business" <?php echo $result['interest'] == 'business' ? 'selected' : ''?>>Бизнес</option>
<option value="design" <?php echo $result['interest'] == 'design' ? 'selected' : ''?>>Дизайн</option>
答案 5 :(得分:1)
您需要让PHP为相应的selected="selected"
标记插入<option>
属性,如下所示:
<?php $result=$userRow['ad_type']; ?>
<select class="form-control" name="ad_type">
<option <?php if($result == 'text'){ echo ' selected="selected"'; } ?> value="text">Text</option>
<option <?php if($result == 'image'){ echo ' selected="selected"'; } ?> value="image">Image</option>
<option <?php if($result == 'video'){ echo ' selected="selected"'; } ?> value="video">Video</option>
<option <?php if($result == 'htmladsense'){ echo ' selected="selected"'; } ?> value="htmladsense">Html Adsense</option>
</select>
答案 6 :(得分:0)
简单的简捷方法就是
//after pulling your content from database
$value = interest['value'];
<option value="seo" <?php echo ($value == "seo") ? 'selected = "selected"' : '' ;?> >SEO и Блоговодство</option>
...
我希望这对您有帮助
答案 7 :(得分:0)
采用可重用性的另一种方式(如WordPress中使用的)Link to Wordpress code是:
<select name='result[interest]' style="width:400px">
<option value='SEO' <?php selected($result['interest'], 'SEO'); ?>>SEO</option>
<option value='AUTO' <?php selected($result['interest'], 'AUTO'); ?>>Auto</option>
</select>
selected()函数确定是否选择了该值并设置所选选项。
/**
* Outputs the html selected attribute.
*
* Compares the first two arguments and if identical marks as selected
*
* @since 1.0.0
*
* @param mixed $selected One of the values to compare
* @param mixed $current (true) The other value to compare if not just true
* @param bool $echo Whether to echo or just return the string
* @return string html attribute or empty string
*/
function selected( $selected, $current = true, $echo = true ) {
return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
selected()函数依次调用下面的帮助器函数,以比较并确定是否选择了该值。返回“ selected = selected”或“。”。
/**
* Private helper function for checked, selected, disabled and readonly.
*
* Compares the first two arguments and if identical marks as $type
*
* @since 2.8.0
* @access private
*
* @param mixed $helper One of the values to compare
* @param mixed $current (true) The other value to compare if not just true
* @param bool $echo Whether to echo or just return the string
* @param string $type The type of checked|selected|disabled|readonly we are doing
* @return string html attribute or empty string
*/
function __checked_selected_helper( $helper, $current, $echo, $type ) {
if ( (string) $helper === (string) $current ) {
$result = " $type='$type'";
} else {
$result = '';
}
if ( $echo ) {
echo $result;
}
return $result;
}