![](/img/trans.png)
[英]how to set the selected value tag <select> html from database in php?
[英]How to set selected value of HTML select box with PHP
我有下一塊模板:
<select name="interest">
<option value="seo">SEO и Блоговодство</option>
<option value="auto">Авто</option>
<option value="business">Бизнес</option>
<option value="design">Дизайн</option>
...
並將結果值存儲在$result['interest']
中。
如何使用 PHP 將option
元素標記為選中?
謝謝!
手動方式.....
<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>
<?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>
<?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);
?> 這涉及制作一個包含您的列表的字符串,然后使用字符串替換功能找到正確的選項並將選定的選項添加到標簽中。 如果使用 XHTML,您將需要使用 selected="selected"。
http://sandbox.onlinephpfunctions.com/code/37eb8f5a213fe5a252cd4da6712f3db0c5558ae3
<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>
<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>
您需要讓 PHP 為適當的<option>
標簽插入selected="selected"
屬性,如下所示:
<?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>
另一種方式(在 WordPress 中使用) 鏈接到使用可重用性的Wordpress 代碼是:
<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;
}
簡單的速記方法是
//after pulling your content from database
$value = interest['value'];
<option value="seo" <?php echo ($value == "seo") ? 'selected = "selected"' : '' ;?> >SEO и Блоговодство</option>
...
我希望這會有所幫助
不需要寫任何東西。 只需輸入以下代碼
`<div class="form-group">
<label for="title">Blog Trending Or Not ?</label><br>
<select class="custom-select" id="inputGroupSelect01" name="interest">
<option value="seo"<?php if($row['is_trending']=="seo"){echo "selected";} ?>>SEO и Блоговодство</option>
<option value="auto"<?php if($row['is_trending']=="auto"){echo "selected";} ?>>Авто</option>
<option value="business"<?php if($row['is_trending']=="business"){echo "selected";} ?>>Бизнес</option>
<option value="design"<?php if($row['is_trending']=="design"){echo "selected";} ?>>Дизайн</option>
</select>
</div>`
通過設置一個包含您的選項數據的數組來保持您的腳本干燥和清潔。 循環遍歷數組並將數據打印到帶有占位符的模板字符串中,以避免丑陋、笨拙的插值、連接和內聯條件。
代碼:(演示)
$interests = [
'seo' => 'SEO и Блоговодство',
'auto' => 'Aвто',
'business' => 'Бизнес',
'design' => 'Дизайн',
];
$selected = 'business';
echo '<select name="interest">';
foreach ($interests as $value => $text) {
printf(
'<option value="%s"%s>%s</option>',
$value,
$selected === $value ? ' selected' : '',
$text
);
}
echo '</select>';
對於任何付出額外努力來生成帶有適當制表符的行分隔選項的人,有幾種方法可以在 PHP 中打印時添加制表符和換行符,但我不會在這里演示它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.