2013-02-22 173 views
-1

我需要幫助才能從我的複選框組獲取數據,並使用多維數組選項來反映在我的發佈頁(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> 
?> 

這給我從後元的輸出是這樣的: admin screenshot

這是從我的帖子頁面的輸出。 : post page screenshot

請幫忙!..我不是程序員,希望你能分享我很多細節的答案。謝謝你提前!

+1

http://php.net/foreach將讓你開始。 – 2013-02-22 19:26:21

+1

這很好,你**需要**有一個輸出... [你有什麼嘗試](http://www.whathaveyoutried.com) – Kermit 2013-02-22 19:27:11

+0

那麼究竟是什麼問題?任何具體的事情不能按預期工作? – sth 2013-02-22 19:27:55

回答

0

所有你需要的是使用var_dump($array)指向一個數組來檢查你需要從數組字段中獲取數據的位置和數據。

第二你需要得到foreach()函數。比如你要搶一組數據:

'priority' => 'high',應儘可能例如:

這將導致作爲數組的值: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; 
     } 
    } 

} 
+0

對不起,沒有說清楚... – 2013-02-23 05:48:45

+0

嗨馬林,謝謝你的答覆非常感謝,但我仍無法弄清楚你的答案。我更新了我的問題,以清楚我想要什麼,也附上了截圖鏈接。 – 2013-02-23 18:23:06