InitPHP (A PHP Framework) 用户手册
第6章 6.7.15 无限极分类

接口描述:

Tree无限分类类。属于InitPHP框架扩展类,需要通过$this->getLibrary()方法获取

使用方法:

$tree = $this->getLibrary('tree'); 

实例:

$tree = $this->getLibrary('tree'); 
$arr = array(      
	1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目'),      
	2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目'),      
	3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目'),      
	4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目'),      
	5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目'),      
	6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目'),      
	7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目')     
 ); 
//$arr = 数组,$mid = 父级id,$selectid = 选中的id,$code = HTML代码, $prefix = 前缀,$selected = 选中的样式 
$tree->get_tree($arr, $mid = 0, $selectid = 5); 
//获取子级别 
$tree->get_child($arr, 0); 
//获取父级别 
$tree->get_parent($arr, 5);

具体类:

if (!defined('IS_INITPHP')) exit('Access Denied!');   
/*********************************************************************************
 * InitPHP 2.1 国产PHP开发框架  扩展类库-无限极分类
 *-------------------------------------------------------------------------------
 * 版权所有: CopyRight By initphp.com
 * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
 *-------------------------------------------------------------------------------
 * $Author:zhuli
 * $Dtime:2011-10-09 
***********************************************************************************/
class treeInit {
	
	private $parentid = 'parentid';
	private $id       = 'id';
	private $name     = 'name';
	
	/**
	 * 无限级分类树-初始化配置
	 * @param  array $config array('parentid'=>'', 'id' => '', 'name' =>'name')
	 * @return string|array
	 */
	public function init($config = array()) {
		if (!is_array($config)) return false;
		$this->parentid = (isset($config['parentid'])) ? $config['parentid'] : $this->parentid;
		$this->id = (isset($config['id'])) ? $config['id'] : $this->id;
		$this->name = (isset($config['name'])) ? $config['name'] : $this->name;
		return true;
	}
	
	/**
	 * 无限级分类树-获取树
	 * @param  array $tree 树的数组
	 * @param  int   $mid  初始化树时候,代表ID下的所有子集
	 * @param  int   $selectid  选中的ID值
	 * @param  string  $code  代码
	 * @param  string  $prefix  前缀
	 * @param  string  $selected  选中
	 * @return string|array
	 */
	public function get_tree($tree, $mid = 0, $selectid = 5, $code = "", $prefix = '|-', $selected = 'selected') {
		if (!is_array($tree)) return '';
		$temp = array();
		$string = '';
		foreach ($tree as $k => $v) {
			if ($v[$this->parentid] == $mid) {
				$id = $v[$this->id];
				$name = $v[$this->name];
				$selecteds = ($id == $selectid) ? $selected : '';
				eval("\$temp_code = \"$code\";");//转化
				$string .=  $temp_code;
				$string .=  $this->get_tree($tree, $v[$this->id], $selectid, $code, '  ' . $prefix);
			}
		}
		return $string ;
	}
	
	/**
	 * 无限级分类树-获取子类
	 * @param  array $tree 树的数组
	 * @param  int   $id   父类ID
	 * @return string|array
	 */
	public function get_child($tree, $id) {
		if (!is_array($tree)) return array();
		$temp = array();
		foreach ($tree as $k => $v) {
			if ($v[$this->parentid] == $id) {
				$temp[] = $v;
			}
		}
		return $temp;
	}
	
	/**
	 * 无限级分类树-获取父类
	 * @param  array $tree 树的数组
	 * @param  int   $id   子类ID
	 * @return string|array
	 */
	public function get_parent($tree, $id) {
		if (!is_array($tree)) return array();
		$temp = array();
		foreach ($tree as $k => $v) {
			$temp[$v[$this->id]] = $v;
		}
		$parentid = $temp[$id][$this->parentid];
		return $temp[$parentid];
	}
	
}