无限级菜单父节点查询所有子节点
Jun 06, 2016 pm 07:31 PM
thinkphp
unlimited
Inquire
node
menu
写了一个Thinkphp的节点工具,通过传入一个父id查询旗下的子节点。 ThinkPHP ?phpclass Tree{static public $treeList=array();//存放无限极分类结果static public $childNode=array();//存放父节点和父节点下面的子节点//无限级分类排序public function crea
写了一个Thinkphp的节点工具,通过传入一个父id查询旗下的子节点。 ThinkPHP
<?php class Tree{ static public $treeList=array();//存放无限极分类结果 static public $childNode=array();//存放父节点和父节点下面的子节点 //无限级分类排序 public function create($data,$pid = 0,$level = 1){ foreach($data as $key => $value){ if($value['pid']==$pid){ $value['level'] = $level; self::$treeList[] = $value; unset($data[$key]); self::create($data,$value['id'],$level+1); } } return self::$treeList; } /******************************************************************************/ /** * 根据父节点,查询父节点下面的子节点 * @param int $tid tree表ID */ public function findChild($tid){ self::$childNode[]=(int)$tid; $tree=M('Tree'); $map['id']=array('eq',$tid); $allTree=$tree->field('id,pid')->select(); //查询tree表 self::findArrayNode($tid, $allTree); return self::$childNode;//返回所有节点 } /** * * @param int $id 节点ID * @param array $list tree表所有的id,pid */ public function findArrayNode($id,$list){ foreach ($list as $key => $val){ if ($id==$val['pid']){ self::$childNode[]=(int)$val['id']; self::findArrayNode($val['id'], $list); //递归,传入新节点ID } } } /******************************************************************************/ //根据findChild()的返回结果,组装sql语句,用于查询news表 public function createSql($arr=array()){ $sql=""; foreach ($arr as $key => $val){ $sql.="tid=$val or "; } $sql=trim($sql); //清除空格 $sql=trim($sql,'or'); //清除最后的or $sql=trim($sql); //清除空格 return $sql; } /******************************************************************************/ /** * 根据tid,获取where的sql * @param int $tid */ public function getSql($tid){ return self::createSql(self::findChild($tid)); } } /* 用法 import('ORG.Util.Tree'); $list=Tree::create($data); 排序前的数据结构 id pid 1 0 2 0 3 1 4 3 排序后的数据结构 id pid level 1 0 1 3 1 2 4 3 3 2 0 1 */ ?>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to check your academic qualifications on Xuexin.com

12306 How to check historical ticket purchase records How to check historical ticket purchase records

There are several versions of thinkphp

Which one is better, laravel or thinkphp?

How to check the activation date on Apple mobile phone
