2013-04-22 35 views
1

我有一些問題讓樹形菜單從下往上工作。 我已經有一個腳本從上往下工作,工作正常。PHP樹形菜單,自底向上

這是我的表的一個非常簡化的版本:

+-----+-----------+--------------------+ 
| uid | parent_id | page_address  | 
+-----+-----------+--------------------+ 
| 1 | 0   | index.php   | 
| 2 | 0   | login.php   | 
| 3 | 2   | dashboard.php  | 
| 4 | 3   | bookings.php  | 
| 5 | 3   | documents.php  | 
| 6 | 4   | changebookings.php | 
| 7 | 4   | activities.php  | 
+-----+-----------+--------------------+ 

的page_address領域是獨一無二的。

我可以計算出用戶當前所在的頁面,例如changebookings.php

我然後像一個菜單,如下所示:

login.php 
    dashboard.php 
    bookings.php 
     changebookings.php 
     activities.php 
    documents.php 

然而,最近我有這麼遠是下面的樹:

login.php 
    bookings.php 
     changebookings.php 

正如你所看到的,我的劇本目前只返回實際的父母,而不是目前在父鏈接列表。

對於那些感興趣的,我使用的腳本總是在這篇文章的底部。

有沒有更簡單的方法來根據需要獲取自下而上樹? 非常感謝

菲爾


編輯: 我終於拿到了代碼工作,爲在這個帖子誰絆倒未來的用戶,我已經添加了功能如下:

$dataRows = $databaseQuery->fetchAll();  // Get all the tree menu records 

$dataRows = $result->fetchAll(PDO::FETCH_ASSOC); 

foreach($dataRows as $row) 
{ 
    if($row['link_address']==substr($_SERVER['PHP_SELF'], 1, strlen($_SERVER['PHP_SELF'])-1)) 
    { 
     $startingId = $row['parent_id']; 
    } 
} 

$menuTree = $this->constructChildTree($dataRows, $startingId); 


private function constructChildTree(array $rows, $parentId, $nesting = 0) 
{ 
    $menu = array(); 

    if(!in_array($nesting, $this->nestingData)) 
    { 
     $this->nestingData[] = $nesting; 
    } 

    foreach($rows as $row) 
    { 
     if($row['parent_id']==$parentId && $parentId!=0) 
     { 
      $menu[] = $row['link_address']; 

      $newParentId = $this->getNextParent($rows, $row['parent_id']); 

      $parentChildren = $this->constructChildTree($rows, $newParentId, ($nesting+1)); 

      if(count($parentChildren)>0) 
      { 
       foreach($parentChildren as $menuItem) 
       { 
        $menu[] = 'NESTING' . $nesting . '::' . $menuItem; 
       } 
      } 
     } 
    } 

    return $menu; 
} 


private function getNextParent($rows, $parentId) 
{ 
    foreach($rows as $row) 
    { 
     if($row['uid']==$parentId) 
     { 
      return $row['parent_id']; 
     } 
    } 
} 
+0

[存儲分層數據庫中的數據(http://www.sitepoint.com/hierarchical-data-database-2/) – DarkBee 2013-04-22 11:23:03

回答

1

沒有讀你的代碼,你應該這樣做:

1)獲取當前頁面,看父母的ID。

2)使用該父ID加載全部。

3)使用當前家長ID作爲ID獲取下一個家長ID。

4)如果新的父母ID!= 0,則轉到步驟2傳遞新的父母ID。

聽起來像你只需要編輯你的腳本,以包括所有的頁面與給定的ID作爲他們的父母ID。

+0

我會接受這個答案,因爲它是有幫助的走向重新寫代碼最終使其按預期工作! – 2013-04-22 12:16:32

+0

酷:)我已經做了類似的Javascript的東西,所以我知道的步驟,但沒有時間去通過你的代碼,並寫下來! – MatthewMcGovern 2013-04-22 12:23:25

1
<?PHP 
$sql = "SELECT * FROM TABLE WHERE table parent_id=0"; 
$result = mysql_query($sql); 
while($perant_menu = mysql_fetch_array($result)) 
{ 
    echo display_child($perant_menu["uid"],$perant_menu["page_address"]); 
} 

// Recursive function 
function display_child($parent_id,$name) 
{ 
    $sql= "SELECT * FROM table where parent_id = $parent_id"; 
    $result = mysql_query($sql); 
    if(mysql_num_rows($result)>0) 
    { 
     while($menu = mysql_fetch_array($result)) 
     { 
      echo display_child($menu["id"],$menu["page_address"]); 
     } 
    } 
    else 
    { 
     echo $name; 
    } 
} 
?> 
+0

不幸的是我不能使用這段代碼,因爲它使用了不推薦的mysql *功能。此外,我試圖限制對服務器的SQL查詢的數量,不幸的是,這個答案,每個父母檢索另一個查詢將通過發送。 我喜歡迴應的時間:) – 2013-04-22 12:21:02