下面是我的一个表结构:
CREATE TABLE menu (
menuid int(11) NOT NULL AUTO_INCREMENT,
menuname varchar(100) NOT NULL DEFAULT '',
menulink varchar(100) NOT NULL DEFAULT '',
menuparentId int(11) NOT NULL DEFAULT '0',
menuhasChild smallint(1) NOT NULL DEFAULT '0',
menustatus smallint(1) NOT NULL DEFAULT '1',
menuorder int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (menuid)
)我使用了一个递归函数来创建一个菜单结构,但在这里失败了:
function categoriesTree($id=0){
$s = "SELECT * FROM menu WHERE menuparentId = '".$id."'
ORDER BY menuorder, menuid ";
$rid = $this->db->query($s)->result_array();
$treeArray = array();
foreach($rid as $row){
$treeArray[$row['menuid']] = $row;
if($row['menuhasChild']==1){
$treeArray[$row['menuid']] = $this->categoriesTree(); //results in Fatal error: Maximum function nesting level of '100' reached, aborting!
}
}
retrun $treeArray;
}此方法是CodeIgniter模型类中模型的一部分。有没有更好的方法来创建这棵树?
发布于 2011-05-11 17:16:38
我认为您必须将id作为参数添加到函数调用中。
$this->categoriesTree($row['menuid']) 否则,您每次调用的函数都是完全相同的。
发布于 2011-05-11 16:34:36
是的,有一个更好的方法。所谓的改进的预序树遍历算法。你可以通过谷歌搜索找到大量的信息,我相信堆栈溢出也是如此。
这样做的好处是,您只需使用一个查询就可以获取整个子树。选择会很快,但修改会更重。
发布于 2011-11-15 18:42:38
这是最好的例子..这是第一个答案的更正形式。
function categoriesTree($id=0) {
$s = "SELECT * FROM design_menu WHERE menuparentId = '" . $id . "'
ORDER BY menuorder, menuid ";
$rid = $this->db->query($s)->result_array();
$treeArray = array();
foreach ($rid as $row) {
$treeArray[$row['menuid']] = $row;
if ($row['menuhasChild'] == 1) {
$treeArray[$row['menuname']] = $this->categoriesTree($row['menuid']); //results in Fatal error: Maximum function nesting level of '100' reached, aborting!
}
}
return $treeArray;
}https://stackoverflow.com/questions/5961377
复制相似问题