我想优化一个多对多的设计,它将返回一个像结构树。数据是这样的:PHP mySQL结构输出树
页表:ID,标题,副标题,类 网页连接器表:ID,PAGE_ID,PARENT_ID,部分
如果部分= X,对于具有孩子每个父母其中一节= X标记应该是这样的:
--- start parent 1 ---
-- if first parent echo *
<>* parent page id | parent title<>
--- parent 1 children ---
<>child page id | child title<>
<>child page id | child title<>
<><>child page id | child title<>
--- end parent 1 ---
--- start parent 2 ---
-- if first parent echo *
<>parent page id | parent title<>
--- parent 2 children ---
<>child page id | child title<>
<>child page id | child title<>
<>child page id | child title<>
--- end parent 2 ---
这种实现似乎运作良好,但我似乎无法弄清楚如何修改函数返回标记如上:
// Menu builder function, parentId 0 is the root
function buildMenu($parent, $menuData)
{
$html = "";
if (isset($menuData['parents'][$parent]))
{
$html .= "
<ul>\n";
foreach ($menuData['parents'][$parent] as $itemId)
{
if(!isset($menuData['parents'][$itemId]))
{
$html .= "<li>\n <a href='".$menuData['items'][$itemId]['link']."'>".$menuData['items'][$itemId]['label']."</a>\n</li> \n";
}
if(isset($menuData['parents'][$itemId]))
{
$html .= "
<li>\n <a href='".$menuData['items'][$itemId]['link']."'>".$menuData['items'][$itemId]['label']."</a> \n";
$html .= buildMenu($itemId, $menuData);
$html .= "</li> \n";
}
}
$html .= "</ul> \n";
}
return $html;
}
echo buildMenu(0, $menuData);