PHP unlimited classification-implementation of left and right values

Source: Internet
Author: User
: This article mainly introduces PHP unlimited classification-implementation of left and right values. For more information about PHP tutorials, see. Contains (move multiple nodes; move a single node; delete multiple nodes; delete a single node; add a node), with a database table structure attached ~ If you have any questions, leave a message (www.webyang.net ).

I. db SQL statement // db used for php unlimited classification create table tree (id int (10) not null primary key auto_increment, name varchar (255) not null, lft int (10) not null default 0, rgt int (10) not null default 0, status int (1) not null default 0, index lft ('lft '), index rgt ('rgt '), index status ('status') charset utf8; insert into tree value (null, 'food', 0 ); insert into tree value (null, 'fruit', 0); insert into tree value (null, 'red', 0); insert into tree value (null, 'Cherry', 0); insert into tree value (null, 'yellow', 0); insert into tree value (null, 'banana, 0); insert into tree value (null, 'meat', 12, 17, 0); insert into tree value (null, 'Beef ', 13, 14, 0 ); insert into tree value (null, 'pork', 15,16, 0); 2. php file
 Primary key, 'root' => Name) select a parent node (insert the largest parent node when null) * @ param array $ ndata = array ('id' => primary key, 'root' => Name) select a sibling node (not required if no sibling node exists) * @ param array $ cdata = array ('id' => primary key, 'root' => Name) select a node to be moved */function move_tree_all ($ pdata = array (), $ ndata = array (), $ cdata = array ()) {$ cid = $ cdata ['id']? Intval ($ cdata ['id']): ''; $ croot = $ cdata ['root']; if (! $ Cid &&! $ Croot) return; // The user-dependent judgment is required. // 1. cdata cannot be top-level/2. cdata cannot be higher than $ pdata $ adata = get_tree_all ($ cdata ); // Obtain delete_tree_all ($ cdata, 1) of all nodes of the current mobile node; // logically delete all nodes of the current mobile node foreach ($ adata as $ k => $ val) {if ($ k! = 0) {$ pdata = array ('root' => $ val ['parent']); insert_tree ($ pdata, '', $ val ['name'], 1) ;}else {// first insert_tree ($ pdata, $ ndata, $ val ['name'], 1 );}}} /*** used to move a node (excluding subnodes) * @ param array $ pdata = array ('id' => primary key, 'root' => name) select either parent node (insert the largest parent node when it is null) * @ param array $ ndata = array ('id' => primary key, 'root' => name) select one or more sibling nodes (not required when no sibling node exists) * @ param array $ cdata = array ('id' => primary key, 'root' => name) select the node to be moved */function move_tree _ Item ($ pdata = array (), $ ndata = array (), $ cdata = array () {$ cid = $ cdata ['id']? Intval ($ cdata ['id']): ''; $ croot = $ cdata ['root']; if (! $ Cid &&! $ Croot) return; // The user-dependent judgment is required. // 1. the cdata cannot be a top-level if (! $ Croot) {$ SQL = "SELECT name from tree where id = $ cid"; $ result = mysql_query ($ SQL); $ row = mysql_fetch_assoc ($ result ); $ croot = $ row ['name']; unset ($ SQL);} delete_tree_item ($ cdata, 1); insert_tree ($ pdata, $ ndata, $ croot, 1);}/*** used to insert a node * @ param array $ pdata = array ('id' => primary key, 'root' => name) select either parent node (insert the largest parent node when it is null) * @ param array $ ndata = array ('id' => primary key, 'root' => name) select one or more sibling nodes (not required if no sibling node exists) * @ param string $ n Ame string New inserted name * @ param int $ update is blank by default. when it is set to 1, update inserted */function insert_tree ($ pdata = array (), $ ndata = array (), $ name, $ update = '') {if (! $ Name) return; $ pid = $ pdata ['id']? Intval ($ pdata ['id']): ''; $ proot = $ pdata ['root']; $ nid = $ ndata ['id']? Intval ($ ndata ['id']): ''; $ nroot = $ ndata ['root']; // has no parent brother (the smallest child node, last Son of the parent node) if ($ pid | $ proot )&&! ($ Nid | $ nroot) {$ SQL = $ pid? "SELECT lft, rgt FROM tree WHERE id = '{$ pid}';": "SELECT lft, rgt FROM tree WHERE name = '{$ proot }';"; $ result = mysql_query ($ SQL); $ row = mysql_fetch_assoc ($ result); unset ($ SQL); // New node $ lft = $ row ['rgt ']; $ rgt = $ lft + 1; if (! $ Update) {$ SQL = "insert into tree values (null, '{$ name}', $ lft, $ rgt, 0 );"; $ sql1 = "update tree set rgt = rgt + 2 where rgt >={ $ row ['rgt ']}"; $ sql2 = "update tree set lft = lft + 2 where lft >={ $ row ['rgt ']}";} else {$ SQL = "update tree set lft = $ lft, rgt = $ rgt, status = 0 where name = '{$ name }';"; $ sql1 = "update tree set rgt = rgt + 2 where status = 0 and rgt >={ $ row ['rgt ']}"; $ sql2 = "update tree set lft = Lft + 2 where status = 0 and lft >={ $ row ['rgt ']} ";} mysql_query ($ sql1); mysql_query ($ sql2 ); mysql_query ($ SQL); // last add new data} // if ($ pid | $ proot) & ($ nid | $ nroot )) {$ SQL = $ nid? "SELECT lft, rgt FROM tree WHERE id = '{$ nid}';": "SELECT lft, rgt FROM tree WHERE name = '{$ nroot }';"; $ result = mysql_query ($ SQL); $ row = mysql_fetch_assoc ($ result); unset ($ SQL); // New node $ lft = $ row ['lft ']; $ rgt = $ lft + 1; if (! $ Update) {$ SQL = "insert into tree values (null, '{$ name}', $ lft, $ rgt, 0 );"; $ sql1 = "update tree set rgt = rgt + 2 where rgt >={ $ row ['lft ']};"; $ sql2 = "update tree set lft = lft + 2 where lft >={ $ row ['lft ']};";} else {$ SQL = "update tree set lft = $ lft, rgt = $ rgt, status = 0 where name = '{$ name }';"; $ sql1 = "update tree set rgt = rgt + 2 where status = 0 and rgt >={ $ row ['lft ']};"; $ sql2 = "update tree set Lft = lft + 2 where status = 0 and lft >={ $ row ['lft ']}; ";}mysql_query ($ sql1); mysql_query ($ sql2 ); mysql_query ($ SQL); // last add new data} // if (! ($ Pid | $ proot )&&! ($ Nid | $ nroot) {$ SQL = "SELECT max ('rgt ') as rgt FROM tree;"; $ result = mysql_query ($ SQL ); $ row = mysql_fetch_assoc ($ result); unset ($ SQL); // New node $ lft = 1; $ rgt = $ row ['rgt '] + 2; if (! $ Update) {$ SQL = "insert into tree values (null, '{$ name}', $ lft, $ rgt, 0 );"; $ sql1 = "update tree set rgt = rgt + 1"; $ sql2 = "update tree set lft = lft + 1 ";} else {$ SQL = "update tree set lft = $ lft, rgt = $ rgt, status = 0 where name = '{$ name }';"; $ sql1 = "update tree set rgt = rgt + 1 where status = 0"; $ sql2 = "update tree set lft = lft + 1 where status = 0 ";} mysql_query ($ sql1); mysql_query ($ sql2); mysql_que Ry ($ SQL); // last add new data}/*** used to delete a node (including subnodes) * @ param array $ data = array ('id' => primary key, 'root' => Name) * @ param int $ update is empty by default, when the value is 1, the logical deletion */function delete_tree_all ($ data, $ update = '') {$ id = $ data ['id']? Intval ($ data ['id']): ''; $ root = $ data ['root']; if (! $ Id &&! $ Root) return; $ SQL = $ id? "SELECT lft, rgt FROM tree WHERE id = '{$ id}';": "SELECT lft, rgt FROM tree WHERE name = '{$ root }';"; $ result = mysql_query ($ SQL); $ row = mysql_fetch_assoc ($ result); unset ($ SQL ); $ middle = $ row ['rgt ']-$ row ['lft'] + 1; if (! $ Update) {$ SQL = "delete from tree where lft '". $ row ['lft ']. "'AND '". $ row ['rgt ']. "'"; $ sql1 = "update tree set rgt = rgt-{$ middle} where rgt> {$ row ['rgt']}"; $ sql2 = "update tree set lft = lft-{$ middle} where lft >{$ row ['rgt ']}";} else {$ SQL = "update tree set status = 1 where lft '". $ row ['lft ']. "'AND '". $ row ['rgt ']. "'"; $ sql1 = "update tree set rgt = rgt-{$ midd Le} where status = 0 and rgt> {$ row ['rgt ']} "; $ sql2 = "update tree set lft = lft-{$ middle} where status = 0 and lft> {$ row ['rgt ']}";} mysql_query ($ SQL ); mysql_query ($ sql1); mysql_query ($ sql2);}/*** used to delete a node (excluding subnodes) * @ param array $ data = array ('id' => primary key, 'root' => Name) * @ param int $ update is empty by default, when the value is 1, the logical deletion */function delete_tree_item ($ data, $ update = '') {$ id = $ data ['id']? Intval ($ data ['id']): ''; $ root = $ data ['root']; if (! $ Id &&! $ Root) return; $ SQL = $ id? "SELECT id, lft, rgt FROM tree WHERE id = '{$ id}';": "SELECT id, lft, rgt FROM tree WHERE name = '{$ root }'; "; $ result = mysql_query ($ SQL); $ row = mysql_fetch_assoc ($ result); unset ($ SQL); if (! $ Update) {$ SQL = "delete from tree where id = {$ row ['id'] };"; $ sql1 = "update tree set rgt = rgt-1, lft = lft-1 where lft> {$ row ['lft ']} and rgt <{$ row ['rgt']} "; $ sql2 = "update tree set lft = lft-2 where lft >{$ row ['rgt ']}"; $ sql3 = "update tree set rgt = rgt-2 where rgt >{$ row ['rgt ']}";} else {$ SQL = "update tree set status = 1 where id = {$ row ['id']};"; $ sql1 = "update tree set rgt = rgt- 1, lft = lft-1 where status = 0 and lft >{$ row ['lft ']} and rgt <{$ row ['rgt']} "; $ sql2 = "update tree set lft = lft-2 where status = 0 and lft> {$ row ['rgt ']}"; $ sql3 = "update tree set rgt = rgt-2 where status = 0 and rgt> {$ row ['rgt ']}" ;}mysql_query ($ SQL ); mysql_query ($ sql1); // can do or not do just right, but not do load empty 2 number in middle mysql_query ($ sql2); mysql_query ($ sql3 );} /*** use Obtain all nodes * @ param array $ data = array ('id' => primary key, 'root' => name), select either */function get_tree_all ($ data) {$ id = $ data ['id']? Intval ($ data ['id']): ''; $ root = $ data ['root']; if (! $ Id &&! $ Root) return; $ SQL = $ id? "SELECT lft, rgt FROM tree WHERE id = '{$ id}';": "SELECT lft, rgt FROM tree WHERE name = '{$ root }';"; $ result = mysql_query ($ SQL); $ row = mysql_fetch_assoc ($ result); $ adata = array (); // All Data $ right = array (); // count $ prev = array (); $ result = mysql_query ("SELECT id, name, lft, rgt FROM tree WHERE lft '". $ row ['lft ']. "'AND '". $ row ['rgt ']. "'Order BY lft ASC;"); while ($ row = mysql_fet Ch_assoc ($ result) {if (count ($ right)> 0) {while ($ right [count ($ right)-1] <$ row ['rgt ']) {// check whether we should remove the node from the stack. array_pop ($ right); array_pop ($ prev) ;}}$ parent = $ prev? End ($ prev): ''; $ adata [] = array ('id' => $ row ['id'], 'name' => $ row ['name'], 'level' => count ($ right), 'parent' => $ parent ); $ right [] = $ row ['rgt ']; $ prev [] = $ row ['name'];} return $ adata ;} /*** display category ** @ param array $ data = array ('id' => primary key, 'root' => Name) select either */function display_tree ($ data) {$ id = $ data ['id']? Intval ($ data ['id']): ''; $ root = $ data ['root']; if (! $ Id &&! $ Root) return; $ SQL = $ id? "SELECT lft, rgt FROM tree WHERE id = '{$ id}';": "SELECT lft, rgt FROM tree WHERE name = '{$ root }';"; $ result = mysql_query ($ SQL); $ row = mysql_fetch_assoc ($ result); $ right = array (); $ result = mysql_query ("SELECT name, lft, rgt FROM tree WHERE lft '". $ row ['lft ']. "'AND '". $ row ['rgt ']. "'Order BY lft ASC;"); while ($ row = mysql_fetch_assoc ($ result) {if (count ($ right)> 0) {// check whether we should remove the node from the stack while ($ right [count ($ right)-1] <$ row ['rgt ']) {array_pop ($ right) ;}} echo str_repeat ('', count ($ right )). $ row ['name']. "\ n"; $ right [] = $ row ['rgt '] ;}} mysql_connect ('localhost', 'root ','') or die ('connect error'); mysql_select_db ('test') or die ('database error'); mysql_query ('set names utf8 '); display_tree (array ('root' => 'food'); // display_tree (array ('root' => 'bigbos ')); // move_tree_all ($ pdata = array ('root' => 'fruit'), $ ndata = array ('root' => 'red '), $ cdata = array ('root' => 'meat'); // move_tree_all ('','', $ cdata = array ('root' => 'meat'); // move_tree_item ('','', array ('root' => 'red ')); // move_tree_item (array ('root' => 'red'), array ('root' => 'cherry '), array ('root' => 'fruit'); // delete_tree_all (array ('root' => 'yellow ')); // delete_tree_all (array ('root' => 'meat'); // delete_tree_item (array ('root' => 'meat ')); // insert_tree ('','', 'bigboss'); // insert_tree (array ('root' => 'red'), '', 'dalao '); // insert_tree (array ('root' => 'red'), array ('root' => 'cherry'), 'baddalao '); // insert_tree (array ('root' => 'failed'), array ('root' => 'red'), 'redbother '); display_tree (array ('root' => 'food '));

The above introduces the PHP unlimited classification-implementation of left and right values, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.