We usually use recursion to implement infinitus classification. We all know that recursion is very inefficient. The following describes an improved forward-order traversal tree algorithm, which does not apply to recursion to implement infinitus classification, it is more efficient to achieve tree-level structure with large data volumes. For details, see www.phpddt.comphpiterate-algorithm.html without CREATETABLEIFNOTEXI
We usually use recursion to implement infinitus classification. We all know that recursion is very inefficient. The following describes an improved forward-order traversal tree algorithm, which does not apply to recursion to implement infinitus classification, it is more efficient to achieve tree-level structure with large data volumes. For details, see: http://www.phpddt.com/php/iterate-algorithm.html without CREATE TABLE IF NOT EXI
We usually use recursion to implement infinitus classification. We all know that recursion is very inefficient. The following describes an improved forward-order traversal tree algorithm, which does not apply to recursion to implement infinitus classification, it is more efficient to achieve tree-level structure with large data volumes.
See http://www.phpddt.com/php/iterate-algorithm.html for details <无>
Create table if not exists 'category '('id' int (11) not null AUTO_INCREMENT, 'title' varchar (50) not null, 'lft' int (11) not null, 'rgt 'int (11) not null, 'order' int (11) not null comment 'sorted', 'create _ time' int (11) not null, primary key ('id') ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 12 ;? ---- 'Category '--? Insert into 'category '('id', 'title', 'lft', 'rgt ', 'order', 'create _ Time') VALUES (1, 'Top topic ', 1, 20, 1, 1261964806), (2, 'edited category', 16, 19, 50,126 4586212), (4, 'Company product ', 10, 15, 50,126 4586249), (5, 'honor qualification ', 8, 9, 50,126 4586270), (6, 'Data download', 6, 7, 50,126 4586295 ), (7, 'talent recruiters ', 4, 5, 50,126 4586314), (8, 'message board', 2, 3, 50,126 4586884), (9, 'President', 17, 18, 50,126 7771951), (10, 'subclassification', 11, 14, 0, 1400044841), (11, 'php Point-to-http://www.phpddt.com ', 12, 13, 0, 1400044901 );
Load-> database ();} public function view () {$ lists = $ this-> db-> order_by ('lft ', 'asc ') -> get ('category ')-> result_array (); // The right value of the two adjacent records. The first right value is greater than the second value. This is his parent class. // we use an array to store the right value of the previous record, compare it with the right value of the record. If the former is smaller than the latter, it indicates that it is not a parent-child relationship. Then, use array_pop to pop up the array. Otherwise, the two loops are retained, no recursion $ parent = array (); $ arr_list = array (); foreach ($ lists as $ item ){? If (count ($ parent) {while (count ($ parent)-1> 0 & $ parent [count ($ parent) -1] ['rgt '] <$ item ['rgt']) {array_pop ($ parent );}}? $ Item ['destpath'] = count ($ parent); $ parent [] = $ item; $ arr_list [] = $ item ;} // display the tree structure foreach ($ arr_list as $ a) {echo str_repeat ('--', $ a ['depath']). $ a ['title'].'
';}}/***** The insert operation simply finds its parent node, and adds 2 to the left and right values of the nodes whose left and right values are greater than the left values of the parent node, then insert the current node. The left and right values are the parent node's left values plus one and the plus two */public function add () {// get the parent class id $ parent_id = 10; $ parent_category = $ this-> db-> where ('id', $ parent_id)-> get ('category ')-> row_array (); #1. add 2 $ this-> db-> set ('lft ', 'lft + 2', FALSE) to the left and right values of a node greater than the left value of the parent node) -> where (array ('lft> '=> $ parent_category ['lft'])-> update ('category '); $ this-> db-> set ('rgt ', 'rgt + 2', FALSE) -> where (array ('rgt> '=> $ parent_category ['lft'])-> update ('category '); #2. insert a new node $ this-> db-> insert ('category', array ('title' => 'subcategory' of the new category ', 'lft '=> $ parent_category ['lft'] + 1, 'rgt '=> $ parent_category ['lft'] + 2, 'order' => 0, 'create _ time' => time (); echo 'add success ';}/*** Delete ** #1. after the deleted node is deleted, the right value is subtracted from the left value and 1 is added. The value $ width = $ rgt-$ lft + 1 is obtained. * #2. delete all nodes between left and right values * #3. modify the condition to all nodes whose values are greater than the right of the current node and subtract the left and right values from $ width */public function delete () {// obtain a category by category id $ id = 3; $ category = $ this-> db-> where ('id', $ id)-> get ('category ') -> row_array (); // calculate $ width = $ category ['rgt ']-$ category ['lft'] + 1; #1. delete this category $ this-> db-> delete ('category ', array ('id' => $ id); #2. delete all categories between left and right values $ this-> db-> delete ('category ', array ('lft>' => $ category ['lft '], 'lft <'=> $ category ['rgt']); #3. modify values of other nodes $ this-> db-> set ('lft ', "lft-{$ width}", FALSE) -> where (array ('lft> '=> $ category ['rgt'])-> update ('category '); $ this-> db-> set ('rgt ', "rgt-{$ width}", FALSE) -> where (array ('rgt> '=> $ category ['rgt'])-> update ('category '); echo 'delete success ';} // edit, public function edit () {// needless to say, use id to edit $ id = 2; $ this-> db-> update ('category ', array ('title' => 'edited category'), array ('id' => $ id); echo 'edit success ';}}