Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.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_Loops_Foreach - Fatal编程技术网

Php 如何设置我的默认选项选择?

Php 如何设置我的默认选项选择?,php,wordpress,loops,foreach,Php,Wordpress,Loops,Foreach,我有一个主题选项页面和一个获取我的类别的循环。这段代码运行良好,我可以保存我的选项。代码如下所示: function drop_elements_function(){ $my_cats = get_categories(); $i = 1; foreach( $my_cats as $my_cat ) : $my_categories[$my_cat->cat_ID] = array( 'value' => $my_cat->cat_ID, 'label'

我有一个主题选项页面和一个获取我的类别的循环。这段代码运行良好,我可以保存我的选项。代码如下所示:

function drop_elements_function(){
$my_cats = get_categories(); 
$i = 1;
foreach( $my_cats as $my_cat ) :
$my_categories[$my_cat->cat_ID] = array(
    'value' => $my_cat->cat_ID,
    'label' => $my_cat->cat_name
);
$i++;
endforeach;

$options = get_option('sandbox_theme_social_options');

echo'<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';

foreach ( $my_categories as $category ){
    $label = $category['label'];
    $selected = '';
    if ( $options['Drop_Elements'] == $category['value'] )
        $selected = 'selected="selected"';
        echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
    }
echo '</select>';
 //print_r($options['Drop_Elements']);
}/*Function end*/
function drop\u elements\u function(){
$my_cats=get_categories();
$i=1;
foreach($my_cats作为$my_cats):
$my\u categories[$my\u cat->cat\u ID]=数组(
“值”=>$my\u cat->cat\u ID,
“标签”=>$my\u cat->cat\u name
);
$i++;
endforeach;
$options=get_option('sandbox_-theme_-social_-options');
回声';
foreach($my_类别作为$category){
$label=$category['label'];
$selected='';
如果($options['Drop_Elements']==$category['value'])
$selected='selected=“selected”';
回显“.$label.”;
}
回声';
//打印($options['Drop_Elements']);
}/*功能端*/
输出如下所示:

问题:现在,当我的主题是第一次安装,我得到的输出像上面的屏幕截图,但显然我的选择还没有保存到数据库(用户必须单击保存按钮,以选择该类别)

所以我想做这样的事情:
1.)用户第一次安装主题,然后输出如下所示:

2.)如果用户无意中选择“选择您的类别”并单击“保存按钮” 输出将再次为:

3.)如果用户选择了真正的类别(而不是“选择您的类别”),则“选择您的类别”将消失:

这是我的尝试,但不起作用:

function drop_elements_function(){
$my_cats = get_categories(); 
$i = 1;
foreach( $my_cats as $my_cat ) :
$my_categories[$my_cat->cat_ID] = array(
    'value' => $my_cat->cat_ID,
    'label' => $my_cat->cat_name
);
$i++;
endforeach;

$options = get_option('sandbox_theme_social_options');

echo'<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';

foreach ( $my_categories as $category ){
    $label = $category['label'];
    $selected = '';
    if ( $options['Drop_Elements'] == $category['value'] ){
        $selected = 'selected="selected"';
        echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
    }
        elseif(!isset($options['Drop_Elements'])){
        $selected = 'selected="selected"';
        echo '<option selected="selected"  value="Choose Your Category">Choose Your Category</option>'; 
        echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . '>' . $label . '</option>';

        }   

}/*Foreach close*/
echo '</select>';
 //print_r($options['Drop_Elements']);
}/*Function end*/
function drop\u elements\u function(){
$my_cats=get_categories();
$i=1;
foreach($my_cats作为$my_cats):
$my\u categories[$my\u cat->cat\u ID]=数组(
“值”=>$my\u cat->cat\u ID,
“标签”=>$my\u cat->cat\u name
);
$i++;
endforeach;
$options=get_option('sandbox_-theme_-social_-options');
回声';
foreach($my_类别作为$category){
$label=$category['label'];
$selected='';
如果($options['Drop_Elements']==$category['value']){
$selected='selected=“selected”';
回显“.$label.”;
}
elseif(!isset($options['Drop_Elements'])){
$selected='selected=“selected”';
echo“选择您的类别”;
回显“.$label.”;
}   
}/*Foreach close*/
回声';
//打印($options['Drop_Elements']);
}/*功能端*/

可以用PHP实现吗?感谢您的帮助!谢谢大家!

我认为您想要做的是如下示例:

echo '<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';    
echo '<option selected="selected"  value="Choose Your Category">Choose Your Category</option>'; 
    foreach ( $my_categories as $category ){
        $label = $category['label'];
        $selected = ( $options['Drop_Elements'] == $category['value'] ) ? " selected='selected' " : "";
        echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';

    }
echo "</select>";
echo';
echo“选择您的类别”;
foreach($my_类别作为$category){
$label=$category['label'];
$selected=($options['Drop_Elements']==$category['value'])?“selected='selected'”:;
回显“.$label.”;
}
回声“;