In this paper, an infinite classification implementation method of PHP with node operation is described. Share to everyone for your reference, specific as follows:
Include (move multiple nodes; move a single node; delete multiple nodes; Delete a single node; new nodes), separate the database table structure
DB Used for PHP infinite category
CREATE TABLE tree (
ID int () NOT NULL primary key auto_increment,
name varchar (255) not NULL,
LfT Int (a) not null default 0,
RGT Int (TEN) NOT null default 0,
status int (1) is not null default 0,
I Ndex lft (' LfT '),
index RGT (' RGT '),
index status (' status ')
) charset UTF8;
Insert into the tree value (null, ' Food ', 1,18,0);
Insert into the tree value (null, ' Fruit ', 2,11,0);
Insert into the tree value (null, ' Red ', 3,6,0);
Insert into the tree value (null, ' Cherry ', 4,5,0);
Insert into the tree value (null, ' Yellow ', 7,10,0);
Insert into the tree value (null, ' Banana ', 8,9,0);
Insert into the tree value (null, ' meat ', 12,17,0);
Insert into the tree value (null, ' Beef ', 13,14,0);
Insert into the tree value (null, ' pork ', 15,16,0);
<?php error_reporting (0); /* 1 Food +------------------------------+ 2 Fruit meat +-------------+ +------------+ 3 Red 6 7 Yellow Beef Pork 4 Cherry 5 8 Banana 9 descendants = (right–left-1 )/2//** * For moving a node (including child nodes) * @param array $pdata = array (' ID ' => primary key, ' root ' => name) two Select a parent node (insert the largest parent node for space) * @param arra Y $ndata = array (' ID ' => primary key, ' root ' => name) two Select a sibling node (not when you don't have a brother) * @param array $cdata = array (' ID ' => primary key, ' root ' => Name) Two Select a node that is currently being moved/function Move_tree_all ($pdata =array (), $ndata =array (), $cdata =array ()) {$cid = $cdata [' id ']? intva
L ($cdata [' ID ']): ';
$croot = $cdata [' Root '];
if (! $cid &&! $croot) return; Need to be judged//1, CDATA cannot be top//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);
Logically deletes 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); /** * is used to move one node (excluding child nodes) * @param array $pdata = array (' ID ' => primary key, ' root ' => name) two Select a parent node (insert the largest parent node for space) * @param arra Y $ndata = array (' ID ' => primary key, ' root ' => name) two Select a sibling node (not when you don't have a brother) * @param array $cdata = array (' ID ' => primary key, ' root ' => Name) Two Select a node that is currently being moved/function Move_tree_item ($pdata =array (), $ndata =array (), $cdata =array ()) {$cid = $cdata [' id ']? intv
Al ($cdata [' ID ']): ';
$croot = $cdata [' Root '];
if (! $cid &&! $croot) return;
Need to be judged//1, CDATA cannot be the 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 the largest parent node for space) * @param array $ndata = arr AY (' id ' => primary key, ' root ' => name) two Select a sibling node (noWhen you have a brother. * @param string $name string new inserted name * @param int $update null by default, insert/function Insert_tree for 1 o'clock Update ($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 node) if (($pid | | | $proot) &&! $nid | | $nroot)) {$sql = $pid?
"Select LfT, RGT from the 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}//have parent brother if (($pid | | $proot) && ($nid | | | $nroot)) {$sql = $nid?
"Select LfT, RGT from the 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 }/** * Used to delete a node (including child nodes) * @param array $data = array (' ID ' => primary key, ' root ' => name) two Select a * @param int $update null by default, tombstone for 1 o'clock
*/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 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);
/** * is used to delete a node (excluding child nodes) * @param array $data = array (' ID ' => primary key, ' root ' => name) two Select a * @param int $update null by default, for 1 o'clock logical Delete * *
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 the 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 > {$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 don't do just right,but does load empty 2 number in middle mysql_query ($sql 2);
mysql_query ($sql 3); /** * is used to get all nodes * @param array $data = array (' ID ' => primary key, ' root ' => name) two Select a/function Get_tree_all ($data) {$id = $data [' id ']?
Intval ($data [' ID ']): ';
$root = $data [' Root '];
if (! $id &&! $root) return; $sql = $id?
"Select LfT, RGT from the 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] < $r
ow[' RGT '] {//Check whether 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; /** * for display classification * @param array $data = array (' ID ' => primary key, ' root ' => name) two Select a/function Display_tree ($data) {$id = $da ta[' id '?
Intval ($data [' ID ']): ';
$root = $data [' Root '];
if (! $id &&! $root) return; $sql = $id?
"Select LfT, RGT from the 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 ');
Display_tree (Array (' root ' => ' Food '));
Display_tree (Array (' 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 (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 ' => ' Fruit '), array (' Root ' => ' Red '), ' redbother ');
Display_tree (Array (' root ' => ' Food '));
More interested in PHP related content readers can view the site topics: "PHP string (String) Usage summary", "PHP Array" Operation Techniques Encyclopedia, "PHP Basic Grammar Primer", "PHP Operations and Operator Usage Summary", " Introduction to PHP object-oriented programming program, "PHP Network Programming Skills Summary", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.