Attention to unlimited classification, Level Classification

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.