PHP infinite classification code support array format direct output menu two ways

  • 2020-05-07 19:18:38
  • OfStack

 
<?php 
/** 
+------------------------------------------------ 
*  Generic tree classes  
+------------------------------------------------ 
* @author yangyunzhou@foxmail.com 
+------------------------------------------------ 
* @date 2010 years 11 month 23 day 10:09:31 
+------------------------------------------------ 
*/ 
class Tree 
{ 

/** 
+------------------------------------------------ 
*  Required to generate a tree structure 2 Dimensional array  
+------------------------------------------------ 
* @author yangyunzhou@foxmail.com 
+------------------------------------------------ 
* @var Array 
*/ 
var $arr = array(); 

/** 
+------------------------------------------------ 
*  To generate a tree structure, you need to modify the symbol, which can be replaced by a picture  
+------------------------------------------------ 
* @author yangyunzhou@foxmail.com 
+------------------------------------------------ 
* @var Array 
*/ 
var $icon = array(' │ ',' ├ ','  └ '); 

/** 
* @access private 
*/ 
var $ret = ''; 

/** 
*  Constructor to initialize the class  
* @param array 2 Dimension array, for example:  
* array( 
* 1 => array('id'=>'1','parentid'=>0,'name'=>'1 Level column 1'), 
* 2 => array('id'=>'2','parentid'=>0,'name'=>'1 Level column 2'), 
* 3 => array('id'=>'3','parentid'=>1,'name'=>'2 Level column 1'), 
* 4 => array('id'=>'4','parentid'=>1,'name'=>'2 Level column 2'), 
* 5 => array('id'=>'5','parentid'=>2,'name'=>'2 Level column 3'), 
* 6 => array('id'=>'6','parentid'=>3,'name'=>'3 Level column 1'), 
* 7 => array('id'=>'7','parentid'=>3,'name'=>'3 Level column 2') 
* ) 
*/ 
function tree($arr=array()) 
{ 
$this->arr = $arr; 
$this->ret = ''; 
return is_array($arr); 
} 

/** 
*  I get the parent series  
* @param int 
* @return array 
*/ 
function get_parent($myid) 
{ 
$newarr = array(); 
if(!isset($this->arr[$myid])) return false; 
$pid = $this->arr[$myid]['pid']; 
$pid = $this->arr[$pid]['pid']; 
if(is_array($this->arr)) 
{ 
foreach($this->arr as $id => $a) 
{ 
if($a['pid'] == $pid) $newarr[$id] = $a; 
} 
} 
return $newarr; 
} 

/** 
*  I get subseries  
* @param int 
* @return array 
*/ 
function get_child($myid) 
{ 
$a = $newarr = array(); 
if(is_array($this->arr)) 
{ 
foreach($this->arr as $id => $a) 
{ 
if($a['pid'] == $myid) $newarr[$id] = $a; 
} 
} 
return $newarr ? $newarr : false; 
} 

/** 
*  Get the current position array  
* @param int 
* @return array 
*/ 
function get_pos($myid,&$newarr) 
{ 
$a = array(); 
if(!isset($this->arr[$myid])) return false; 
$newarr[] = $this->arr[$myid]; 
$pid = $this->arr[$myid]['pid']; 
if(isset($this->arr[$pid])) 
{ 
$this->get_pos($pid,$newarr); 
} 
if(is_array($newarr)) 
{ 
krsort($newarr); 
foreach($newarr as $v) 
{ 
$a[$v['id']] = $v; 
} 
} 
return $a; 
} 

/** 
* ------------------------------------- 
*  You get the tree structure  
* ------------------------------------- 
* @author yangyunzhou@foxmail.com 
* @param $myid  To get this ID All the child levels below  
* @param $str  Generate the tree structure base code ,  For example, : "<option value=\$id \$select>\$spacer\$name</option>" 
* @param $sid  The selected ID,  For example, when you're doing a tree drop down  
* @param $adds 
* @param $str_group 
*/ 
function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '') 
{ 
$number=1; 
$child = $this->get_child($myid); 
if(is_array($child)) { 
$total = count($child); 
foreach($child as $id=>$a) { 
$j=$k=''; 
if($number==$total) { 
$j .= $this->icon[2]; 
} else { 
$j .= $this->icon[1]; 
$k = $adds ? $this->icon[0] : ''; 
} 
$spacer = $adds ? $adds.$j : ''; 
$selected = $id==$sid ? 'selected' : ''; 
@extract($a); 
$parentid == 0 && $str_group ? eval("\$nstr = \"$str_group\";") : eval("\$nstr = \"$str\";"); 
$this->ret .= $nstr; 
$this->get_tree($id, $str, $sid, $adds.$k.' ',$str_group); 
$number++; 
} 
} 
return $this->ret; 
} 

/** 
*  Same as above 1 Methods the similar , But multiple choices are allowed  
*/ 
function get_tree_multi($myid, $str, $sid = 0, $adds = '') 
{ 
$number=1; 
$child = $this->get_child($myid); 
if(is_array($child)) 
{ 
$total = count($child); 
foreach($child as $id=>$a) 
{ 
$j=$k=''; 
if($number==$total) 
{ 
$j .= $this->icon[2]; 
} 
else 
{ 
$j .= $this->icon[1]; 
$k = $adds ? $this->icon[0] : ''; 
} 
$spacer = $adds ? $adds.$j : ''; 

$selected = $this->have($sid,$id) ? 'selected' : ''; 
@extract($a); 
eval("\$nstr = \"$str\";"); 
$this->ret .= $nstr; 
$this->get_tree_multi($id, $str, $sid, $adds.$k.' '); 
$number++; 
} 
} 
return $this->ret; 
} 

function have($list,$item){ 
return(strpos(',,'.$list.',',','.$item.',')); 
} 

/** 
+------------------------------------------------ 
*  Formatted array  
+------------------------------------------------ 
* @author yangyunzhou@foxmail.com 
+------------------------------------------------ 
*/ 
function getArray($myid=0, $sid=0, $adds='') 
{ 
$number=1; 
$child = $this->get_child($myid); 
if(is_array($child)) { 
$total = count($child); 
foreach($child as $id=>$a) { 
$j=$k=''; 
if($number==$total) { 
$j .= $this->icon[2]; 
} else { 
$j .= $this->icon[1]; 
$k = $adds ? $this->icon[0] : ''; 
} 
$spacer = $adds ? $adds.$j : ''; 
@extract($a); 
$a['title'] = $spacer.' '.$a['title']; 
$this->ret[$a['id']] = $a; 
$fd = $adds.$k.' '; 
$this->getArray($id, $sid, $fd); 
$number++; 
} 
} 

return $this->ret; 
} 
} 
?> 

Related articles: