Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从下拉菜单中从数组中获取选定值?_Php_Wordpress_Drop Down Menu_Selectedvalue - Fatal编程技术网

Php 从下拉菜单中从数组中获取选定值?

Php 从下拉菜单中从数组中获取选定值?,php,wordpress,drop-down-menu,selectedvalue,Php,Wordpress,Drop Down Menu,Selectedvalue,我觉得我真的错过了一些东西。我在一个简短的(wordpress)脚本上进行了一些手动定制,并尝试将设置为“selected”(已选中)。为了实现这一点,我试图找出$term->id=是否与所选选项值(也是一个id)相似。到目前为止一切正常,但我似乎无法获得提交的。它所说的只是“数组” <form method="get" action="<?php echo trailingslashit( home_url() ); ?>" class="custsearch"> &l

我觉得我真的错过了一些东西。我在一个简短的(wordpress)脚本上进行了一些手动定制,并尝试将
设置为“selected”(已选中)。为了实现这一点,我试图找出
$term->id=是否与所选选项值(也是一个id)相似。到目前为止一切正常,但我似乎无法获得提交的
。它所说的只是“数组”

<form method="get" action="<?php echo trailingslashit( home_url() ); ?>" class="custsearch">
<select name="listing_cat[]">
<option value="1" disabled="disabled" <?php if(!count($_GET)>0 || !$_GET["listing_cat"] ){ echo 'selected="selected"';} ?>>Pls choose</option>
        <?php
        $cat_args = array(
        'orderby'           => 'id', 
        'parent'        => 0,
        'hide_empty'    => false           
    );
        $terms = get_terms('listing_category', $cat_args );
        foreach ($terms as $term) {
            printf( '<option class="level-0" value="' . $term->term_id .'"');
            if($_GET["listing_cat"] == $term->term_id) { echo 'selected="selected"'; }
            printf( '>%s</option>', $term->slug, $term->name );
        }

        ?>
</select>
</form>
下面是我的脚本。有人知道我做错了什么吗?要清楚的是,这里似乎都是关于
$\u GET[“listing\u cat”]
。另外,当我试图在
foreach
中打印
$\u GET[“listing_cat”]
时,它输出的所有内容都是“array”

<form method="get" action="<?php echo trailingslashit( home_url() ); ?>" class="custsearch">
<select name="listing_cat[]">
<option value="1" disabled="disabled" <?php if(!count($_GET)>0 || !$_GET["listing_cat"] ){ echo 'selected="selected"';} ?>>Pls choose</option>
        <?php
        $cat_args = array(
        'orderby'           => 'id', 
        'parent'        => 0,
        'hide_empty'    => false           
    );
        $terms = get_terms('listing_category', $cat_args );
        foreach ($terms as $term) {
            printf( '<option class="level-0" value="' . $term->term_id .'"');
            if($_GET["listing_cat"] == $term->term_id) { echo 'selected="selected"'; }
            printf( '>%s</option>', $term->slug, $term->name );
        }

        ?>
</select>
</form>
>请选择

好的,这是我所需要的一切。。相当合乎逻辑;)。我将其放入一个函数中,并需要in_array()来检查:

//category in dropdown
function the_refine_category_ui2() {
    $cat_args = array(
    'orderby'           => 'id', 
    'parent'        => 0,
    'hide_empty'    => false           
);
    $terms = get_terms('listing_category', $cat_args ); 
foreach ($terms as $term) {

   printf( '<option class="level-0"');
   if( in_array( $term->term_id, $_GET["listing_cat"] )) { echo 'selected ';} 

    printf('value="' . $term->term_id .'"');
            printf( '>%s' . $term->term_id .'</option>', $term->slug, $term->name );


}
}
//下拉列表中的类别
函数的类型为{
$cat_args=数组(
'orderby'=>'id',
“父项”=>0,
“hide_empty”=>false
);
$terms=get_terms('listing_category',$cat_args);
foreach($terms作为$term){
printf(“%s”。$term->term_id.”,$term->slug,$term->name);
}
}

这可能是完全不相关的,但您确实意识到select的名称中有括号?这当然让我觉得它解释的值应该是一个数组。如果它是一个多选的,那么它是get中的一个数组是有意义的,不管是哪种方式,请查看清单3中的每一个数组