我试图用expand 和 collapsetree
来显示数据。JQuery
For example my database
Parent A has AA, AB, AC children
Parent AA has AAA, AAB, AAC
Parent AC has ACA, ACB, ACD
etc.
所以我使用递归来显示数据库中的所有数据。
但是我还需要声明 JQuery,所以我使用相同的Recursion
代码来生成脚本
我想问的是,有没有办法使用递归一次来生成这两种东西?数据显示在<body>
标签内,JQuery 在<head>
标签处声明。
因为我觉得做同样的事情两次会浪费内存。
这是我的<body>
代码
<?
$temp = "";
$parent = "";
$level = 0;
loadFolders($parent, $level, $temp);
function loadFolders($parent, $level, $temp){
$getLevelSql = mysql_query("select * from folders where parent='$parent' and level='$level'");
$level++;
while ($getLevelSqlRow = mysql_fetch_array($getLevelSql)){
$parentID = $getLevelSqlRow['id'];
$child = $getLevelSqlRow['child'];
$checkChild = mysql_query("select * from folders where parent='$parentID' and level='$level' ");
if (mysql_num_rows($checkChild)){
?>
<div class="level1 redFont" id="levelID<?echo $getLevelSqlRow['id'];?>">
<?echo "$child";?>
</div>
<div class="level2" id="childID<?echo $getLevelSqlRow['id'];?>">
<?
loadFolders($parentID, $level, $temp);
?>
</div>
<?
} else {
?>
<div><?echo "$child";?></div>
<?
}
}
}
?>
这是我的<head>
代码
<script>
$(document).ready(function(){
<?
include "config.php";
$temp = "";
$parent = "";
$level = 0;
loadFolder($parent, $level, $temp);
function loadFolder($parent, $level, $temp){
$getLevelSql = mysql_query("select * from folders where parent='$parent' and level='$level'");
$level++;
while ($getLevelSqlRow = mysql_fetch_array($getLevelSql)){
$parentID = $getLevelSqlRow['id'];
$child = $getLevelSqlRow['child'];
$checkChild = mysql_query("select * from folders where parent='$parentID' and level='$level' ");
if (mysql_num_rows($checkChild)){
?>
$("#levelID<?echo $getLevelSqlRow['id'];?>").click(function(){
$("#childID<?echo $getLevelSqlRow['id'];?>").slideToggle("slow");
});
<?
loadFolder($parentID, $level, $temp);
?>
<?
}
}
}
?>
});
</script>
提前致谢