2011-12-06 57 views
1

我想弄清楚爲什麼它只顯示名單,事件和社會對象中的最後一個child_links。我已經包含了具有數組函數的print_r的站點。任何幫助,將不勝感激。只顯示數組中的最後一項

http://kansasoutlawwrestling.com/

function getSubMenuPages() 
{ 
    $this->db->select('id'); 
    $this->db->where('short_name', 'mainnav'); 
    $query = $this->db->get('site_menu_structures'); 
    $menu_id = $query->row()->id; 

    $this->db->select('id, short_name, is_category'); 
    $this->db->where('menu_structure_id', $menu_id); 
    $query = $this->db->get('site_menu_structures_links'); 

    if ($query->num_rows() > 0) 
    { 
     $linksArray = $query->result(); 
     echo "<pre>"; 
     print_r($linksArray); 
     echo "</pre>"; 
     foreach ($linksArray as $key => $link) 
     { 
      if ($link->is_category == 'Yes') 
      { 
       $this->db->select('link_name, site_content_pages_id, link_url'); 
       $this->db->where('site_menu_structures_links_id', $link->id); 
       $query = $this->db->get('site_menu_structures_links_children'); 
       if ($query->num_rows() > 0) 
       { 
        foreach ($query->result() as $row) 
        { 
         $site_content_page_id = $row->site_content_pages_id; 
         $linksArray[$key]->child_links = array(); 
         if ($site_content_page_id != 0) 
         { 
          $this->db->select('content_page_name, permalink'); 
          $this->db->where('id', $site_content_page_id); 
          $query = $this->db->get('site_content_pages'); 
          if ($query->num_rows() > 0) 
          { 
           $row = $query->row(); 
           $linksArray[$key]->child_links = array(
                   'link_name' => $row->content_page_name, 
                   'link_url' => $row->permalink 
                  ); 
          } 
         } 
         else 
         { 
          $linksArray[$key]->child_links = array(
                   'link_name' => $row->link_name, 
                   'link_url' => $row->link_url 
                  ); 
         } 
        } 
       } 
      } 
     } 
    } 

    return $linksArray; 

} 

回答

4

當你循環這裏:

foreach ($query->result() as $row) 
        { 
         $site_content_page_id = $row->site_content_pages_id; 
         $linksArray[$key]->child_links = array(); 
         if ($site_content_page_id != 0) 
         { 

你的另一循環內,所以在這個循環的每個通道這裏的關鍵$保持不變。

因此,第一外部迴路, $ key ='key1';我將用一些僞代碼給的想法:

foreach results as row: 
    (loop nr 1): 
    linksarray['key1'] = empty array. 
    linksarray['key1'] = value; 
    (loop nr 2): 
    linksarray['key1'] = empty array 
    linksarray['key1'] = value2 
endforeach; 

第二外環,$關鍵=「密鑰2」

foreach results as row: 
     (loop nr 1): 
     linksarray['key2'] = empty array. 
     linksarray['key2'] = value; 
     (loop nr 2): 
     linksarray['key2'] = empty array 
     linksarray['key2'] = value2 
    endforeach; 

我可能有錯誤看起來是在所有這些循環,這是晚上在這裏,我有點累了:)


哦,一個忠告:你的代碼變得容易有點難以閱讀;一個小的改進就是省略表名(因爲它沒有必要),尤其是當它很長時間的時候;此外,在AR中,您可以省略「from」子句和鏈式方法。

所以,對於前夫這樣的:

$this->db->select('site_menu_structures_links_children.link_name, site_menu_structures_links_children.site_content_pages_id, site_menu_structures_links_children.link_url'); 
$this->db->from('site_menu_structures_links_children'); 
$this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id); 
$query = $this->db->get(); 

可以很容易地改寫爲:

$query = $this->db->select('link_name,site_content_pages_id,link_url') 
        ->where('site_menu_structures_links_id',$link->id) 
        ->get('site_menu_structures_links_children'); 

,否則可能會幫助您識別出了一般的代碼流,察覺出各次試驗

UPDATE

試試這個

function getSubMenuPages() 
{ 
    $this->db->select('id'); 
    $this->db->where('short_name', 'mainnav'); 
    $query = $this->db->get('site_menu_structures'); 
    $menu_id = $query->row()->id; 

    $this->db->select('id, short_name, is_category'); 
    $this->db->where('menu_structure_id', $menu_id); 
    $query2 = $this->db->get('site_menu_structures_links'); 

    if ($query2->num_rows() > 0) 
    { 
     $linksArray = $query2->result(); 
     echo "<pre>"; 
     print_r($linksArray); 
     echo "</pre>"; 
     foreach ($linksArray as $key => $link) 
     { 
      if ($link->is_category == 'Yes') 
      { 
       $this->db->select('link_name, site_content_pages_id, link_url'); 
       $this->db->where('site_menu_structures_links_id', $link->id); 
       $query3 = $this->db->get('site_menu_structures_links_children'); 
       if ($query3->num_rows() > 0) 
       { 
        $linksArray[$key]->child_links = array(); 
        foreach ($query3->result() as $row) 
        { 
         $site_content_page_id = $row->site_content_pages_id; 
         //$linksArray[$key]->child_links = array(); 
         if ($site_content_page_id != 0) 
         { 
          $this->db->select('content_page_name, permalink'); 
          $this->db->where('id', $site_content_page_id); 
          $query4 = $this->db->get('site_content_pages'); 
          if ($query4->num_rows() > 0) 
          { 
           $row = $query4->row(); 
           $linksArray[$key]->child_links[]['link_name'] = $row->content_page_name; 
           $linksArray[$key]->child_links[]['link_url'] = $row->permalink; 
          } 
         } 
         else 
         { 
          $linksArray[$key]->child_links[]['link_name'] = $row->link_name; 
          $linksArray[$key]->child_links[]['link_url'] = $row->link_url; 
         } 
        } 
       } 
      } 
     } 
    } 

    return $linksArray; 

} 

基本上我把財產分配$linksArray[$key]->child_links = array();移到循環之外。被跟蹤的另一個循環,這都不可能有更多的價值,我創建了一個索引每個循環:

$linksArray[$key]->child_links[]['link_name'] = $row->content_page_name; 
$linksArray[$key]->child_links[]['link_url'] = $row->permalink; 
+0

偉大的工作,但它不是沉入我很好。我試圖找出如何將它應用於我自己的。 –

+0

謝謝我做到了這一點,並將用我的新代碼進行更新。仍然沒有修復。 –

+0

@JeffDavidson用一個建議更新了我的答案;不能測試它,但試試 –

1

嘗試更換的代碼塊:

$this->db->select('link_name, site_content_pages_id, link_url'); 
$this->db->where('site_menu_structures_links_id', $link->id); 
$query = $this->db->get('site_menu_structures_links_children'); 
if ($query->num_rows() > 0) 
{ 
    $linksArray[$key]->child_links = array(); 

    foreach ($query->result() as $row) 
    { 
     $site_content_page_id = $row->site_content_pages_id; 

     if ($site_content_page_id != 0) 
     { 
      $this->db->select('content_page_name, permalink'); 
      $this->db->where('id', $site_content_page_id); 
      $query2 = $this->db->get('site_content_pages'); 
      if ($query2->num_rows() > 0) 
      { 
       $row2 = $query2->row(); 
       array_push($linksArray[$key]->child_links, array(
               'link_name' => $row2->content_page_name, 
               'link_url' => $row2->permalink 
              )); 
      } 
     } 
     else 
     { 
      array_push($linksArray[$key]->child_links, array(
               'link_name' => $row->link_name, 
               'link_url' => $row->link_url 
              )); 
     } 
    } 
}