我有一个递归树视图,它从数据库中提取所有信息,并希望了解如何使其可折叠。我尝试使用 Jquery 和 Javascript,但都没有任何效果。不确定是否可能只是我的错误或其他原因。
这是我的基本代码:
<body>
<?php
mysql_connect('localhost', 'root');
mysql_select_db('test');
$qry="SELECT * FROM treeview_items";
$result=mysql_query($qry);
$arrayCategories = array();
while($row = mysql_fetch_assoc($result)){
$arrayCategories[$row['id']] = array("parent_id" => $row['parent_id'], "name" =>
$row['title']);
}
//createTree($arrayCategories, 0);
function createTree($array, $currentParent, $currLevel = 0, $prevLevel = -1) {
foreach ($array as $categoryId => $category) {
if ($currentParent == $category['parent_id']) {
if ($currLevel > $prevLevel) echo " <ul> ";
if ($currLevel == $prevLevel) echo " </li> ";
echo '<li id="'.$categoryId.'" onclick=child(this.id);>
<span>'.$category['name'].'</span>';
if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
$currLevel++;
createTree ($array, $categoryId, $currLevel, $prevLevel);
$currLevel--;
}
}
if ($currLevel == $prevLevel) echo " </li> </ul> ";
}
?>
<div id="content" class="general-style1">
<?php
if(mysql_num_rows($result)!=0)
{
?>
<ul>
<li id="0" class="root"><span>Categories</span>
<?php createTree($arrayCategories, 0); ?>
</li>
</ul>
<?php
}
?>
</div>
</body>
</html>
任何帮助表示赞赏!