상향식에서 작동하도록 트리 메뉴를 가져 오는 데 문제가 있습니다. 나는 하향식으로 작업 할 스크립트가 이미 있는데 잘 작동한다.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'];
}
}
}
[데이터베이스에 보관 계층 데이터 (http://www.sitepoint.com/hierarchical-data-database-2/) – DarkBee