Attention to unlimited classification, Level Classification
Infinitus classification is designed based on Recursive Algorithms and special data tables.
My code
1/** 2 * Get ordered menu Information 3 * use Recursive Method 4 */5 public function getHierarchicalMenus ($ pid, $ condition = array (), $ cid = 0) {6 $ data = array (); 7 if (! Isset ($ condition ['status']) {8 $ condition ['status'] = array ('neq',-1 ); 9} 10 $ condition ['parentid'] = $ pid; 11 $ menus = $ this-> _ db-> where ($ condition)-> order ("listorder desc ") -> select (); 12 if ($ menus) {13 foreach ($ menus as $ v) {14 $ res = $ this-> getHierarchicalMenus ($ v ['menu _ id'], $ condition, $ cid + 1); 15 if ($ res) {16 $ v ['child '] = $ res; 17 $ v ['cid'] = $ cid; 18} else {19 $ v ['child'] = 0; 20} 21 $ v ['cid'] = $ cid; 22 $ data [] = $ v; 23} 24} 25 return $ data; 26}
Instructor code
1 public function getTree() 2 { 3 $data = $this->select(); 4 return $this->_reSort($data); 5 } 6 private function _reSort($data, $parent_id=0, $level=0, $isClear=TRUE) 7 { 8 static $ret = array(); 9 if($isClear)10 $ret = array();11 foreach ($data as $k => $v)12 {13 if($v['parent_id'] == $parent_id)14 {15 $v['level'] = $level;16 $ret[] = $v;17 $this->_reSort($data, $v['id'], $level+1, FALSE);18 }19 }20 return $ret;21 }
Obviously different, the infinite level I designed is to use array for nesting, so there is a big problem in front-end display,
Nested layers of the corresponding display requires several layers of judgment plus loop, however, the teacher uses a two-dimensional array, through the level
To determine the hierarchy. The sequence remains unchanged, and all data is recorded before recursion to reduce
Increase the number of database operations.