创建树结构所需的PHP递归帮助 [英] PHP recursion help needed to create a tree structure

查看:92
本文介绍了创建树结构所需的PHP递归帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我拥有的表结构:

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)
)

我正在使用递归函数从中创建菜单结构,但在这里失败:

I am using a recursive function to create a menu structure from this and fail here:

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模型类中模型的一部分.有没有更好的方法来创建树?

This method is part of a model in CodeIgniter model class. Is there a better way to create the tree?

推荐答案

我认为您必须在函数调用中添加id作为参数.

I think you have to add the id as parameter in your function call.

$this->categoriesTree($row['menuid']) 

否则,您每次都完全相同地调用该函数.

Otherwise you call the function exactly the same every time.

这篇关于创建树结构所需的PHP递归帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆