2013-04-22 7 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

답변

1
: 내가 마지막이 게시물에 우연히 미래의 사용자를 위해 일할 수있는 코드를 가지고, 나는 기능 아래에 추가 한

코드를 읽지 않고 다음을 수행해야합니다.

1) 상위 페이지에서 현재 페이지를 가져옵니다.

2) 모두를 해당 상위 ID로로드하십시오.

3) 현재 학부모 ID를 ID로 사용하여 다음 학부모 ID를 가져옵니다.

4) 새 상위 ID! = 0 인 경우 2 단계로 이동하여 새 상위 ID를 전달하십시오.

주어진 ID를 가진 모든 페이지를 부모 ID로 포함하도록 스크립트를 편집하기 만하면됩니다.

+0

는쪽으로 도움이되었다 나는이 대답을 받아 들일 겁니다 다시 쓰는 결국 코드가 예상대로 작동하게되었습니다. –

+0

Cool :) 자바 스크립트에서 비슷한 것을 했으므로 단계를 알고 있었지만 코드를 작성하고 작성해야 할 시간이 없었습니다! – MatthewMcGovern

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 쿼리의 수를 제한하려고 시도하고 있습니다. 불행히도이 대답으로, 각 부모가 다른 쿼리를 검색 할 수 있도록 보내지는 것입니다. 응답 시간이 짧다고 생각합니다. :) –