duande1146 2015-01-21 03:12
浏览 35

如何使用和访问已保存的数组值

I have a PHP function which returns the values in a PHP list() format. This function returns a list of WordPress taxonomies with a couple of custom entries added manually.

/**
 * Returns array with taxonomies
 */
public function get_taxonomies(){
    $args = array(
        'public'    => true,
        '_builtin'  => false
    );
    $taxonomies = get_taxonomies($args, 'objects');
    $list = array();
    $list[__('Category', $this->prefix)] = 'category';
    $list[__('Tag', $this->prefix)] = 'post_tag';
    foreach($taxonomies as $tax){

        $list[$tax->labels->name] = $tax->name;
    }
    return $list;
}

This function returns the following array values:

Array
(
    [Category] => category
    [Tag] => post_tag
    [Product Categories] => product_cat
    [Product Tags] => product_tag
    [Shipping Classes] => product_shipping_class
)

The first part of the array is the taxonomy name and the second the taxonomy value.

I display these values as a checkbox list and save them in my database as an array.

This is the data that I save in the database for the list of checkboxes. This is what is returned if I print_r() the save data for the list:

Array
(
    [category] => category
    [post_tag] => post_tag
    [product_cat] => product_cat
)

Using the WordPress checked() function I am trying to return the initial list of taxonomies with the values saved in the database marked as HTML "checked".

Here is what I have tried:

// This is where I am trying to get at the saved values
$multi_stored = $options[$id];

$taxonomies = $this->get_taxonomies();
foreach($taxonomies as $name => $label){

    $check_key = $id . '_' . $option;

    $output .= '<input class="checkbox'.$field_class.'" type="checkbox" name="'.$this->prefix.'_options['.$id.']['.$label.']" id="'.$check_key.'" value="'.$label.'" '.checked($multi_stored[$label], $label, false ).' /><label for="'.esc_attr($name).'">'.esc_html($name).'</label><br />';
}

When I run this code I received the following PHP warning.

Notice: Undefined index: product_tag in C:\xampp\htdocs\wpbp-admin.php on line ...

It is giving me this warning for every item in the checkbox list that is NOT checked.

I would like to have the code not return these warnings. I understand it is a matter of using isset but I am not sure where to place it.

Any help is greatly appreciated, thanks.

  • 写回答

1条回答 默认 最新

  • drjgk44268 2015-01-21 03:52
    关注

    I got it to work.

    I change to "Checked" function in the loop to this:

    $multi_stored = $options[$id];
    
    $taxonomies = $this->get_taxonomies();
    foreach($taxonomies as $name => $label){
    
        if(isset($multi_stored[$label])){
            if($label == $multi_stored[$label]){
                $checked = 'checked="checked"';
            } else {
                $checked = '';
            }
        } else {
            $checked = '';
        }
    
        $check_key = $id . '_' . $label;
    
        $output .= '<input class="checkbox'.$field_class.'" type="checkbox" name="'.$this->prefix.'_options['.$id.']['.$label.']" id="'.$check_key.'" value="'.$label.'" '.$checked.' /><label for="'.esc_attr($name).'">'.esc_html($name).'</label><br />';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害