我有数据库categories
表id, name, parent
我目前使用以下方法创建导航栏:
$rsCategories = mysql_query("SELECT * FROM categories WHERE hide = '0' ORDER BY parent, id");
$arrayCategories = array();
while($row = mysql_fetch_assoc($rsCategories)){
$arrayCategories[$row['id']] = array("parent" => $row['parent'], "name" => $row['name']);
}
function createTree($array, $currentParent, $currLevel = 0, $prevLevel = -1) {
foreach ($array as $categoryId => $category) {
if ($currentParent == $category['parent']) {
if ($currLevel > $prevLevel) echo " <ul> ";
if ($currLevel == $prevLevel) echo " </li> ";
echo '<li id="'.$categoryId.'"><a href="/categorie/'. strtolower(str_replace(" ", '_', $category['name'])) .'-'. $categoryId .'/">'. $category['name'] .'</a>';
if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
$currLevel++;
createTree ($array, $categoryId, $currLevel, $prevLevel);
$currLevel--;
}
}
if ($currLevel == $prevLevel) echo " </li> </ul> ";
}`
I need to get full link on third category level like
`href="/category_1/subcaegory_4/ssubcategory_2/"`
For second category level the link should be
`href="/category_1/subcategory_4/"`
and for first level
`href="/category_1/"`
I have tried with `LEFT OUTER JOIN`
`$rsCategories = mysql_query("SELECT *, c.name AS name, c.parent AS parent, c.id AS id, c1.name AS c1_name FROM categories c LEFT OUTER JOIN categories c1 ON c1.id = c.parent WHERE c.hide = '0' AND c1.hide = '0' ORDER BY c.parent, c.id");
但不是工作...
谢谢!