2013-08-06 54 views
1

嘗試使用許多嵌套循環和方法來構建此JavaScript塊,我需要專家幫助。我遍歷所有的'menuitems',並且menu_item_parent 0中的那些是頂級父項,其餘的我構建了較低級別。缺少構建JavaScript對象的字符PHP循環

我該如何將這兩個字符添加到這些循環中?

它缺少一個逗號

, 

每個

{'name' : '','url':'','subs':[ 
{'_id:':'1','title': '','img': ''} 
,{'_id:':'1','title': '','img': ''} 
]} 

功能後

function make_menu_object(){ 
    $menu_name = 'sec_level'; 
    $menu = wp_get_nav_menu_object($menu_name); 
    $menuitems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC')); 
    $objpart=null; 

    $menuitems_count = count($menuitems); 
    $master_counter = 0; 

    foreach($menuitems as $item){ 

     // get page id from using menu item object id 
     $id = get_post_meta($item->ID, '_menu_item_object_id', true); 
     // set up a page object to retrieve page data 
     $page = get_page($id); 
     $link = get_page_link($id); 
     // item does not have a parent so menu_item_parent equals 0 (false)  

     if ($item->menu_item_parent==0){ 
      if($master_counter<>0){$objpart.=']},';} 
      $objpart .= <<<EOT 

'$item->title' : { 
'url':'', 
'sections': [ 

EOT; 
     }else{ 

     $counter=1; 
     $the_star=1; 



     $objpart .= <<<EOT 
{'name' : '$item->title','url':'','subs': [ 

EOT; 
      $args = array('numberposts' => 4, 'offset'=> 0, 'post_type' => 'post', 'category' => $item->object_id); 
      $myposts = get_posts($args); 

      $the_count = count($myposts); 
      foreach($myposts as $post) {//subs output into js object 
       setup_postdata($post); 
       $the_empty_thumbnail = '/wp-content/uploads/2013/03/holder_img_menu.jpg'; 
       $the_thumbnail = get_the_post_thumbnail($post->ID, 'thumbnail'); 
       //$the_thumbnail_img = (!empty($the_thumbnail) ? the_post_thumbnail() : $the_empty_thumbnail); 
       $the_post_title = addslashes($post->post_title); 
       $objpart .= <<<EOT 
{'_id:':'1','title': '','img': ''} 

EOT; 
       if($counter<$the_count){$objpart .= ',';} 
        $counter++; 
       } 


      if($the_star==1||$the_star==$counter){$objpart .=']}';}else{$objpart .=',';} 
       $the_star++; 
     } 

     $master_counter++; 
    } 
$objpart .=']}'; 

return '<pre>'.$objpart.'</pre>'; 

} 

電流輸出

'Item 1' : {'url':'','sections': [ 
    {'name' : '','url':'','subs':[ 
     {'_id:':'1','title': '','img': ''} 
     ,{'_id:':'1','title': '','img': ''} 
     ]} 

    {'name' : '','url':'','subs': [ 
    {'_id:':'1','title': '','img': ''} 
    ,{'_id:':'1','title': '','img': ''} 
    ,{'_id:':'1','title': '','img': ''} 
    ,{'_id:':'1','title': '','img': ''} 
    ]} 

    ]}, 

    'Item 2' : { 
    'url':'', 
    'sections': [ 
    {'name' : '','url':'','subs': [ 
    {'_id:':'1','title': '','img': ''} 
    ]}]} 

所需的輸出

'Item 1' : {'url':'','sections': [ 
    {'name' : '','url':'','subs':[ 
     {'_id:':'1','title': '','img': ''} 
     ,{'_id:':'1','title': '','img': ''} 
     ]} 
    , 
    {'name' : '','url':'','subs': [ 
    {'_id:':'1','title': '','img': ''} 
    ,{'_id:':'1','title': '','img': ''} 
    ,{'_id:':'1','title': '','img': ''} 
    ,{'_id:':'1','title': '','img': ''} 
    ]} 

    ]}, 

    'Item 2' : { 
    'url':'', 
    'sections': [ 
    {'name' : '','url':'','subs': [ 
    {'_id:':'1','title': '','img': ''} 
    ]}]} 
+2

您所需的輸出不是js對象。您應該在PHP中創建對象,然後使用json_encode將其轉換爲js對象。 – Musa

+2

手動構建一個JSON字符串是一個壞主意。速度很慢,容易出錯。將您的對象構建爲PHP數組,然後使用'json_encode()'來生成您的JSON對象。您的代碼太複雜,無法將其重新編碼爲答案。走一走,如果你拿到stuk就回來一個問題。 – 2013-08-06 01:29:57

+0

@MikeW我明白了。我在段數組元素中缺少一個逗號。立即更新問題... – Codex73

回答

1

正如評論你的問題的狀態,你應該在PHP中的對象,然後使用json_encode。

另外正如評論中提到的,它太複雜的代碼來重寫它作爲答案。

但要指出你在正確的方向,一些僞代碼來顯示如何構建PHP對象:

function make_menu_object() { 
    $itemsForJS = new array(); // Initialize an empty array to hold the menu items 

    // Code to retrieve the menu items 
    // ... 

    $currTopLevelNode = null; // Because output from WP is flat list, state is needed 

    foreach ($menuitems as $item) { 
     // Code to retrieve data about the menu item 
     // ... 

     if ($item->menu_item_parent==0) { 
      $currTopLevelNode = new stdClass; // Initialize an empty object 
      $currTopLevelNode->url = ''; // Or something more interesting... 
      $currTopLevelNode->sections = array(); // Empty holder for the sections to come 
      $itemsForJS[$item->title] = $currTopLevelNode; 
     } else { 
      $section = new stdClass; 
      $section->name = ''; // Or something more interesting 
      $section->url = ''; // Or maybe $item->url? 
      $section->subs = array(); // Empty array to hold data for the subs 

      // Code to retrieve post 
      // ... 

      foreach ($mypost as $post) { 
       $sub = new stdClass; // Empty object to hold data for the page 

       // Code to retrieve data about the post 
       // ... 

       $sub->id = '1'; // Or maybe $post->post_id? 
       $sub->title = ''; // Maybe $post->post_title? 
       $sub->url = ''; // Maybe $post->post_url? 

       $section->subs[] = $sub; // Adding the post to the section 
      } 

      $currTopLevelNode->sections[] = $section; // Adding the section to the item 
     } 
    } 
    return $itemsForJS; // This is an associative array, not string as in your code 
} 

使用此功能類似於:

$menuObject = make_menu_object(); 
echo '<pre>'.json_encode($menuObject).'</pre>'; 

。當然,這是僞代碼。例如,它假定收到的第一個$item必須是頂級的 - 否則$currTopLevelNode將爲空,並且代碼將崩潰。錯誤檢查需要。

0
if ($item->menu_item_parent==0){ 
    $objpart .= "]}"; 
} 

之後的第二添加此如果