Home php教程 php手册 无限级菜单父节点查询所有子节点

无限级菜单父节点查询所有子节点

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 Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check your academic qualifications on Xuexin.com

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

How to run thinkphp project

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

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

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

There are several versions of thinkphp

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

How to run thinkphp

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

How to install thinkphp

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Which one is better, laravel or thinkphp?

How to check the activation date on Apple mobile phone How to check the activation date on Apple mobile phone Mar 08, 2024 pm 04:07 PM

How to check the activation date on Apple mobile phone

See all articles