2014-03-27 117 views
0

編輯PHP/MySQL的數據庫,從

產生多維關聯數組,我想製造一點CMS用於測試目的。 我設置創建具有懸垂的陣列的輸出看起來像這樣的嵌套元素導航類:

Array 
(
    [0] => Array 
     (
      [name] => Home 
      [link] => 
     ) 

    [1] => Array 
     (
      [name] => About us 
      [link] => about 
      [children] => Array 
       (
        [0] => Array 
         (
          [name] => Team 
          [link] => team 
         ) 

        [1] => Array 
         (
          [name] => History 
          [link] => history 
         ) 

       ) 

     ) 

    [2] => Array 
     (
      [name] => Contact 
      [link] => contact 
     ) 

) 

它的工作原理相當不錯,到目前爲止,但我最終需要的是具有無限嵌套的數組可能性。事情是這樣的:

Array 
(
    [0] => Array 
     (
      [name] => Home 
      [link] => 
     ) 

    [1] => Array 
     (
      [name] => About us 
      [link] => about 
      [children] => Array 
       (
        [0] => Array 
         (
          [name] => Team 
          [link] => team 
         ) 

        [1] => Array 
         (
          [name] => History 
          [link] => history, 
          [children] => Array 
          (
           [name] => Pictures 
           [link] => pictures 
          ) 
         ) 

       ) 

     ) 

    [2] => Array 
     (
      [name] => Contact 
      [link] => contact 
     ) 

) 

我使用下面的PHP腳本來填充從數據庫中數據的數組:

/** 
* Loops through the children of a page and adds them accordingly to the pages array 
* 
* @param array $parent 
* @param array $children 
*/ 
private function getChildrenPages($parent, $children) { 
    $subpages = array(); 

    foreach ($children as $child) { 
     array_push($subpages, array(
      'name' => $child['name'], 
      'link' => $child['link'] 
     )); 
    } 

    array_push($this->pages, array(
     'name' => $parent['name'], 
     'link' => $parent['link'], 
     'children' => $subpages 
    )); 
} 

/** 
* @return array Returns an multidimensional associative array with all pages 
*/ 
private function fetchPages() { 
    // Prevent multiple db fetches 
    if(!count($this->pages)){ 
     $all_pages = $this->db->get('pages'); 

     for ($i=0; $i < count($all_pages); $i++) { 
      $parent = $all_pages[$i]; 

      // Get children of current item 
      $this->db->where('parent_id', $parent['id']); 
      $children = $this->db->get('pages'); 

      // 
      if(count($children)) { 
       $this->getChildrenPages($parent, $children); 
      } 

      if (!$parent['parent_id'] && !count($children)) { 
       // Append current item without children to pages array 
       array_push($this->pages, array(
        'name' => $parent['name'], 
        'link' => $parent['link'] 
       )); 
      } 
     } 
    } 
} 

這適用於第一和第二位項目。但是如何處理更高層次的項目?我想我必須將我的getChildrenPages()函數轉換爲遞歸函數,但不知道如何在這種情況下做出這一點。有什麼建議麼?

+0

通過在每個行中存儲一個「parent_id」字段,而不是像您在此處的「children」字段中存儲多個id,來存儲像這樣的層次結構通常要容易得多。然後,您可以實際上對有意義的數據進行「加入」,而不必分析字符串。 – Sammitch

+0

@Sammitch你能舉個例子嗎?我認爲'JOIN'用於兩個表的多個表? – enyce12

+1

對於自己加入表沒有規定。 [例子](http://sqlfiddle.com/#!2/b1b81a/2):我不知道爲什麼小提琴沒有顯示那個連接的右側,但相信我,它在那裏。 :I – Sammitch

回答

1

好的,我認爲你是在正確的軌道上。我過去做過類似的事情,除了父母沒有列出孩子,我讓孩子們列出了父母。我想,語義上差別很小,但它會讓事情變得更容易。

列出所有沒有父項的項目。這些將是您的主要導航項目。對於您顯示的每個項目,請檢查是否有任何行聲明此項目爲其父項。如果是這樣,那麼請繼續,並將它們列在它們下面。這裏的所有都是它的!

+0

感謝您的建議。你將如何處理3級菜單?那麼讓我們說,如果「Team」頁面將包含另一個子頁面? – enyce12

+0

我所做的只是將父級設置爲第二級子行的標識。所以想象一下,如果'a','b','c',菜單項和'aa'&'bb'的父母列爲'b'。然後你可以列出'aaa'和'bbb'作爲父'bb'。 – Quixrick

+0

聽起來很合理。你可能會告訴我一些你的代碼嗎? – enyce12