【发布时间】:2011-01-11 20:02:56
【问题描述】:
我有一个带有 parentid 的简单数据库树,我想读取 db 并获得一个像上面这样的数组
Array
(
Title: Category 1
Children => Array
(
=> Title: Category 1.1
=> Title: Category 1.2
Children => Array
(
=> Title: Category 1.2.1
)
)
)
我尝试用上面的代码实现
function getTree($rootid)
{
$result = =mysql_query("select * from tree where parentid='$rootid'");
while ($row = mysql_fetch_array($result)) {
$arr[]=$row["Title"];
getChilds($row["id"]);
}
}
function getChilds($id)
{
$result = =mysql_query("select * from tree where parentid='$id'");
while ($row = mysql_fetch_array($result)) {
//childers nodes here
$arr[]=$row["Title"];
getChilds($row["id"]);
}
}
}
我有一个关于如何将数组传递给递归函数的问题,所以从我写的最后一个节点继续子节点等等。
它在一个类中实现,我知道我必须通过 & $arr 但我不确定如何
任何帮助表示赞赏
谢谢
【问题讨论】: