【问题标题】:How to echo checkbox group with multidimentional array如何用多维数组回显复选框组
【发布时间】:2013-02-24 02:28:42
【问题描述】:

我需要帮助从我的复选框组中获取具有多维数组选项的数据,以反映在我的帖子页面(single.php 代码)中。单选类型运行良好,但复选框组类型却不行。我在底部添加了在我的 single.php 中找到的用于收音机类型的示例代码,它查询数据到我的帖子页面供您参考。

这是我的 metabox.php 代码中的数组:

<?php
// array
$prefix = 'wtf_';
$meta_box = array( 'id' => 'site',
               'title' => 'Program Details',
               'page' => 'post',
               'context' => 'normal',
               'priority' => 'high',
               'fields' => array(
                        array('name' => 'Principal Return',
                                'desc' => 'Principal Return After Expiry or Not',
                                'id' => $prefix . 'principal',
                                'type' => 'radio',
                                'options' => array(
                                        array('name' => '  Yes    ', 'value' => 'Yes-after expiry'),
                                        array('name' => '  No    ', 'value' => 'No-included on the interest')
                                    )
                                ),
                        array(
                        'name' => 'Compounding',
                        'desc' => 'Choose if compounding is allowed or not',
                        'id' => $prefix . 'compounding',
                        'type' => 'radio',
                        'options' => array(
                             array('name' => '  Yes    ', 'value' => 'Allowed'),
                             array('name' => '  No    ', 'value' => 'Not Allowed'),
                             array('name' => '  Re-purchase', 'value' => 'Yes thru re-purchase')
                    )
                                ),
                             array ('name' => 'Payment Processors',  
                                    'desc'  => 'Payment Processsor Accepted',  
                                    'id'    => $prefix.'processors',  
                                    'type'  => 'checkbox_group',  
                                    'options' => array(
                                                    array('label' => ' Liberty Reserve ', 'value' =>'LR'),
                                                    array('label' => ' SolidTrustPay ', 'value' =>'STP'),
                                                    array('label' => ' EgoPay ', 'value' =>'EgoPay'),
                                                    array('label' => ' Perfect Money ', 'value' =>'PM'),
                                                    array('label' => ' Payza ', 'value' =>'Payza'),
                                                    array('label' => ' PayPal ', 'value' =>'PayPal'),
                                                    array('label' => ' Bankwire ', 'value' =>'Bankwire')
                                      ))))

// Callback function to show fields in meta box
function mytheme_show_box() {
global $meta_box, $post;

// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="',    wp_create_nonce(basename(__FILE__)), '" />';

echo '<table class="form-table">';

foreach ($meta_box['fields'] as $field) {
    // get current post meta data
    $meta = get_post_meta($post->ID, $field['id'], true);

    echo '<tr>',
            '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
            '<td>';
    switch ($field['type']) {
    case 'text':
                echo $statetemt;
            break;
        case 'textarea':
                echo $statetemt;
            break;
        case 'select':
                echo $statetemt;
            break;
        case 'radio':
            foreach ($field['options'] as $option) {
                echo $statetemt; }
            break;
        case 'checkbox':
            foreach ($field['options'] as $option) {
                echo $statetemt;}
            break;
        case 'checkbox_group':  
             foreach ($field['options'] as $option) {  
                echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />',$option['label']; }  
                echo '<br /><span class="description">'.$field['desc'].'</span>';  
            break;
    }


//From my single.php code  <<<<=================

<div class="sdinfo"><strong>Principal Return</strong>:<span><?php $principal = get_post_meta(get_the_ID(), 'wtf_principal', true); 
      if (isset($principal[0])) { 
        echo $principal ; 
} else if (isset($principal[1])) {
        $principal = get_post_meta(get_the_ID(), 'wtf_principal', true);
        echo $principal; 
       } else {_e('Not Available');} ?></span></div>
<div class="sdinfo"><strong>Program Started</strong>:<span> <?php $started = get_post_meta(get_the_ID(), 'wtf_started', true); if (isset($started[0])) { echo $started; 
                } else {_e('Not Available');} ?></span></div>
<div class="sdinfo"><strong>Compounding</strong>:<span>
<?php $compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true); 
      if (isset($compounding[0])) { 
        echo $compounding ; 
                } else if (isset($compounding[1])) {
        $compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true);
        echo $compounding; 
      } else if (isset($compounding[2])) {
        $compounding = get_post_meta(get_the_ID(), 'wtf_compounding', true);
        echo $compounding; 
      } else {_e('Not Available');} ?></span></div>
?>

这给了我来自 post meta 的输出,如下所示: admin screenshot

这是我的帖子页面的输出。 : post page screenshot

请帮忙!..我不是程序员,希望你能详细分享我的答案。提前谢谢你!

【问题讨论】:

  • php.net/foreach 将帮助您入门。
  • 很高兴您需要有一个输出...what have you tried
  • 那么你的问题到底是什么?有什么具体的地方没有按预期工作吗?
  • 为了清楚起见,我已经更新了我的问题。希望你能分享你的答案。

标签: php checkbox


【解决方案1】:

您所需要的只是使用var_dump($array) 指向一个数组,以检查您需要从数组字段中获取哪些数据。

其次你需要得到foreach()函数。例如你想获取一个数据数组:

'priority' =&gt; 'high', 应该是例子:

echo $meta_box["priority"] 将作为数组的值:high

如果您不确定并且不熟悉数组,请使用:

foreach ($meta_box as $arr)
{
    var_dump($arr);

    #then play and manipulate how to grab a fields a data:
    foreach ($arr["fields"][""] as $fa)
    {
        var_dump($fa["name"]);
        var_dump($fa["desc"]);
        var_dump($fa["desc"]);
    }

    #...
}

如果您不确定数据数组,请通过var_dump() 了解如何抓取。

支付处理器 LR STP EgoP

echo $meta_box["fields"][""]["name"];

你需要获取一个数组的数据:

foreach ($meta_box["fields"][""]["options"] as $options)
{
    foreach ($options as $options_key)
    {
        foreach ($options_key as $opt_val)
        {
            $result .= ' '.$opt_val;
        }
    }

}

【讨论】:

  • 嗨 Marin,非常感谢您的回答,但我仍然无法弄清楚您的回答。我更新了我的问题以明确我想要什么。还附上了截图链接。
猜你喜欢
  • 2012-11-12
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 2012-09-11
  • 2014-05-29
相关资源
最近更新 更多