我不确定这里发生了什么,我在 localhost 上测试了这段代码,但现在我已经尝试上线了,我收到了这个错误消息:
致命错误:第 24 行 .../functions/tree.php 中允许的内存大小为 67108864 字节已用尽(尝试分配 261900 字节)
我猜代码会导致某种无限递归,但我有点困惑,因为它在 localhost 上运行良好。我花了几天时间试图让它以我想要的方式运行,我不愿意在没有方向的情况下改变事情。关于可能导致此错误和/或如何修复它的任何想法?
<?php
function hasChild($parent_id)
{
$sql = "SELECT COUNT(*) as count FROM categories WHERE parent = '" . $parent_id . "'";
$qry = mysql_query($sql);
$rs = mysql_fetch_array($qry);
return $rs['count'];
}
function CategoryTree($list,$parent,$append)
{
$list = '<li>'.'<a href="inventory.php?cid=' . $parent['id'] . '&cname=' . $parent['cname'] . '">' . $parent['cname'] . '</a>'.'</li>';
if (hasChild($parent['id'])) // check if the id has a child
{
$append++;
$list .= "<ul class='child child".$append."'>";
$sql = "SELECT * FROM categories WHERE parent = '" . $parent['id'] . "'";
$qry = mysql_query($sql);
$child = mysql_fetch_array($qry);
do{
$list .= CategoryTree($list,$child,$append); //this is line 24
}while($child = mysql_fetch_array($qry));
$list .= "</ul>";
}
return $list;
}
function CategoryList()
{
$list = "";
$sql = "SELECT * FROM categories WHERE (parent = 0 OR parent IS NULL)";
$qry = mysql_query($sql);
$parent = mysql_fetch_array($qry);
$mainlist = "<ul class='parent'>";
do{
$mainlist .= CategoryTree($list,$parent,$append = 0);
}while($parent = mysql_fetch_array($qry));
$list .= "</ul>";
return $mainlist;
}
?>