【问题标题】:Blank select option in Form Output表单输出中的空白选择选项
【发布时间】:2015-01-18 19:19:29
【问题描述】:

所有, 我有以下代码可以从 Wordpress 中选择一些类别:

$args = array(
    'type' => 'post',
    'hide_empty' => 0   
    );
$categories = get_categories( $args );

当我打印出数组时,我得到以下输出:

Array ( [0] => stdClass Object ( [term_id] => 4 [name] => Baseball [slug] => baseball [term_group] => 0 [term_taxonomy_id] => 4 [taxonomy] => category [description] => [parent] => 0 [count] => 29 [cat_ID] => 4 [category_count] => 29 [category_description] => [cat_name] => Baseball [category_nicename] => baseball [category_parent] => 0 ) [1] => stdClass Object ( [term_id] => 2 [name] => Football [slug] => football [term_group] => 0 [term_taxonomy_id] => 2 [taxonomy] => category [description] => [parent] => 0 [count] => 4 [cat_ID] => 2 [category_count] => 4 [category_description] => [cat_name] => Football [category_nicename] => football [category_parent] => 0 ) [2] => stdClass Object ( [term_id] => 1 [name] => Uncategorized [slug] => uncategorized [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 21 [cat_ID] => 1 [category_count] => 21 [category_description] => [cat_name] => Uncategorized [category_nicename] => uncategorized [category_parent] => 0 ) )

这是我的用于显示数组内容的 foreach 循环:

foreach($categories as $category){
    if ($category->name != 'Uncategorized') {
        echo '<option value="'.$category->term_id.'"> '.$category->name.'<option>';
    }
}

由于某种原因,foreach 循环在具有值的选项之间添加了一个空白选项。这会是什么?

【问题讨论】:

    标签: php


    【解决方案1】:

    您需要将&lt;option&gt; 替换为&lt;/option&gt;

    foreach($categories as $category){
        if ($category->name != 'Uncategorized') {
            echo '<option value="'.$category->term_id.'"> '.$category->name.'</option>';
        }
    }
    

    HTML 标记始终采用以下形式:

    <foo>...</foo>
    

    有一些不需要子元素或文本的例外:

    <input>
    

    <input/>
    

    【讨论】:

    • 这对我来说是愚蠢的。谢谢。
    • 我们都想念这些东西 :)
    猜你喜欢
    • 2012-04-20
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    相关资源
    最近更新 更多