PHP Infinite Classification-left and right value implementation

Source: Internet
Author: User
Tags cdata
Include (move multiple nodes, move a single node, delete multiple nodes, delete a single node, add a node), attach a database table structure ~ There is a problem can leave a message (www.webyang.net).

DB SQL statement//db used for PHP infinite classification CREATE TABLE tree (ID int) NOT NULL primary key auto_increment, name varchar (255) Not NULL, LFT Int (ten) NOT null default 0, RGT Int (ten) NOT null default 0, status int (1) is not NULL default 0, in Dex lft (' LfT '), Index RGT (' RGT '), index status (' status ')) charset Utf8;insert into the tree value (null, ' food ', 1,18,0); Insert into tree value (null, ' Fruit ', 2,11,0); insert into tree value (null, ' Red ', 3,6,0); insert into tree value (null, ' Cherr Y ', 4,5,0); insert into tree value (null, ' Yellow ', 7,10,0); Insert to tree value (null, ' Banana ', 8,9,0); insert into tree Val UE (NULL, ' meat ', 12,17,0); insert into tree value (null, ' Beef ', 13,14,0); insert into tree value (null, ' pork ', 15,16,0); PHP file
 Primary key, ' root ' = ' name ') two Select a parent node (the largest parent node is inserted when empty) * @param array $ndata = array (' id ' = = ' primary key, ' root ' = ' name ') two Select a sibling node (without a brother) * @param array $cdata = array (' id ' = = Primary key, ' root ' = ' name ') two select the node currently being moved */function Move_tree_all ($pdata =array (), $ndata =    Array (), $cdata =array ()) {$cid = $cdata [' id ']? intval ($cdata [' ID ']): ';    $croot = $cdata [' Root '];    if (! $cid &&! $croot) return; Self-judgment required//1, CDATA cannot be top-level//2, CDATA cannot be higher than $pdata level $adata = Get_tree_all ($cdata); Gets all nodes of the current mobile node Delete_tree_all ($cdata, 1); Tombstone all nodes of the current mobile node foreach ($adata as $k + $val) {if ($k! = 0) {$pdata = array (' root ' + $val [' PA            Rent ']);        Insert_tree ($pdata, ", $val [' name '],1);        } else {//first insert_tree ($pdata, $ndata, $val [' name '],1); }}}/** * is used to move a node (excluding child nodes) * @param array $pdata = array (' id ' = = ' primary key, ' root ' = ' name ') two Select a parent node (insert the largest parent node when empty) * @param array $n data = array (' id ' = + primary key, ' root ' = ' = Name ') Two Select a sibling node (without a brother) * @param array $cdata = Array (' Id ' = = Primary key, ' root ' = ' two ' Select a node currently 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;        Self-judgment required//1, 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);} /** * for inserting a node * @param array $pdata = array (' id ' = = Primary key, ' root ' = ' name ') two Select a parent node (insert maximum parent node when empty) * @param array $ndata = Array (' I d ' = = Primary key, ' root ' = ' two ' Select a sibling node (not when you have no brothers) * @param string $name A newly inserted name * @param int $update default is empty, 1 o'clock update insert */fun    Ction 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 ']; There is no brother (the smallest child node, the last son of the parent) if (($pid | | $proot) &&! $nid | | $nroot) {$sql = $pid?        "Select LfT, RGT from the 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);";            $sql 1 = "Update tree set RGT = rgt+2 where RGT >= {$row [' RGT ']}";        $sql 2 = "Update tree set lft = lft+2 where lft >= {$row [' RGT ']}";            } else {$sql = "update tree set lft= $lft, rgt= $rgt, status=0 where name = ' {$name} ';";            $sql 1 = "Update tree set RGT = rgt+2 where status =0 and RGT >= {$row [' RGT ']}";        $sql 2 = "Update tree set lft = lft+2 where status =0 and LfT >= {$row [' RGT ']}"; } Mysql_querY ($sql 1);        mysql_query ($sql 2); mysql_query ($sql); Last add new Data}//Parent has brother if (($pid | | $proot) && ($nid | | $nroot)) {$sql = $nid?        "Select LfT, RGT from the 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);";            $sql 1 = "Update tree set RGT = rgt+2 where RGT >= {$row [' lft ']};";        $sql 2 = "Update tree set lft = lft+2 where lft >= {$row [' lft ']};";            } else {$sql = "update tree set lft= $lft, rgt= $rgt, status=0 where name = ' {$name} ';";            $sql 1 = "Update tree set RGT = rgt+2 where status = 0 and RGT >= {$row [' lft ']};"; $sql 2 = "Update tree set lft = lft+2 where status = 0 and LfT >= {$row [' lft ']};";        } mysql_query ($sql 1);        mysql_query ($sql 2); mysql_query ($sql); Last add new Data}//No Father no brother (big guy) 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);";            $sql 1 = "Update tree set RGT = rgt+1";        $sql 2 = "Update tree set lft = lft+1";            } else {$sql = "update tree set lft= $lft, rgt= $rgt, status=0 where name = ' {$name} ';";            $sql 1 = "Update tree set RGT = rgt+1 where status = 0";        $sql 2 = "Update tree set lft = lft+1 where status = 0";        } mysql_query ($sql 1);        mysql_query ($sql 2); mysql_query ($sql); Last add new Data}}/** * is used to delete a node (including child nodes) * @param arraY $data = array (' id ' = + primary key, ' root ' = + name) Two select * @param int $update default is null, 1 o'clock Tombstone */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 the 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 between '". $row [' LfT ']. "' and '".        $row [' RGT ']. "'";        $sql 1 = "Update tree set RGT = rgt-{$middle} where RGT > {$row [' RGT ']}";    $sql 2 = "Update tree set lft = lft-{$middle} where LfT > {$row [' RGT ']}"; } else {$sql = "update tree set status = 1 where lft between '". $row [' LfT ']. "' and '".        $row [' RGT ']. "'"; $sql 1 = "Update tree set RGT = rgt-{$middle} where status=0 and Rgt > {$row [' RGT']}";    $sql 2 = "Update tree set lft = lft-{$middle} where status=0 and LfT > {$row [' RGT ']}";    } mysql_query ($sql);    mysql_query ($sql 1); mysql_query ($sql 2);} /** * For deleting a node (excluding child nodes) * @param array $data = array (' id ' = = ' primary key, ' root ' = ' name ') two select one * @param int $update default is null, 1 o'clock Tombstone */functi    On 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 the 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 ']};";        $sql 1 = "Update tree set RGT = Rgt-1,lft = lft-1 where lft > {$row [' LfT ']} and RGT < {$row [' RGT ']}";        $sql 2 = "Update tree set lft = lft-2 where lft > {$row [' RGT ']}"; $sql 3 = "Update tree set RGT = rgt-2 where RGT &GT    {$row [' RGT ']} ";        } else {$sql = "update tree set status = 1 WHERE id = {$row [' id ']};";         $sql 1 = "Update tree set RGT = Rgt-1,lft = lft-1 where status = 0 and LfT > {$row [' LfT ']} and RGT < {$row [' RGT ']}";        $sql 2 = "Update tree set lft = lft-2 where status = 0 and LfT > {$row [' RGT ']}";    $sql 3 = "Update tree set RGT = rgt-2 where status = 0 and RGT > {$row [' RGT ']}";    } mysql_query ($sql);    mysql_query ($sql 1);    Can do or does just right,but not does load empty 2 number in middle mysql_query ($sql 2); mysql_query ($sql 3);} /** * Used to get all nodes * @param array $data = array (' id ' = = Primary key, ' root ' = ' = ' name ') two select one */function get_tree_all ($data) {$id = $dat a[' id ']?    Intval ($data [' ID ']): ';    $root = $data [' Root '];    if (! $id &&! $root) return; $sql = $id?    "Select LfT, RGT from the 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 between '". $row [' LfT ']. "' and '". $row [' RGT ']. "'    ORDER by LfT ASC; ");  while ($row = Mysql_fetch_assoc ($result)) {if (count ($right) > 0) {while ($right [count ($right)-1]                < $row [' RGT ']) {//Check if we should move the node out of 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;} /** * Used to display categories * @param array $data = array (' id ' = = Primary key, ' root ' = ' = ' name ') two Select a */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 between '". $row [' LfT ']. "' and '". $row [' RGT ']. "'    ORDER by LfT ASC; "); while ($row = Mysql_fetch_assoc ($result)) {if (count ($right) > 0) {//Check whether we should move the node out of 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 ');d isplay_tree (Array (' root ' = ' food ');//display_tree (' root ' = ' bigboss ') ;//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 (' root ' = ' Red '), array (' root ' = ' Cherry '), array (' root ' = ' Fruit ')),//delete_tree_all (' root ' = ' Yellow ');// Delete_tree_all (Array (' root ' = ' meat ');//delete_tree_item (' root ' = ' meat ');//insert_tree (",", " Bigboss ');//insert_tree (' root ' = ' red '), ' ', ' Dalao ');//insert_tree (' root ' = ' red '), array (' root ' = ' Cherry '), ' Baddalao '),//insert_tree (' root ' = ' Fruit '), array (' root ' = ' Red '), ' redbother '); Display_tree (Array (' root ' = ' food ');

The above describes the PHP unlimited classification-left and right values to achieve, including the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.