Unlimited classification algorithm based on left and right values

Source: Internet
Author: User
Based on the left and right value infinite classification algorithm, php, because the previously used recursive sorting infinite classification method is not very ideal, so I referred to the foreign and domestic left and right value sorting related methods, I wrote an unlimited classification class based on left and right values. You are welcome to test it.
Based on the left and right value infinite classification algorithm, php, because the previously used recursive sorting infinite classification method is not very ideal, so I referred to the foreign and domestic left and right value sorting related methods, I wrote an unlimited classification class based on the left and right values. You are welcome to test the demo URL.
 Db = $ db; $ this-> tablefix = "om _";} // end func/*** Short description. * Add a new category * Detail description * @ param none * @ global none * @ since 1.0 * @ access private * @ return void * @ update date time */function addsort ($ CatagoryID, $ SortName) {if ($ CatagoryID = 0) {$ Lft = 0; $ Rgt = 1;} else {$ Result = $ this-> checkcatagory ($ CatagoryID ); // Obtain the left value of the parent class. the right value is $ Lft = $ Result ['lft ']; $ Rgt = $ Result ['rgt']; $ this-> db-> query ("update '". $ this-> tablefix. "catagory 'set 'lft '= 'lft' + 2 WHERE 'lft '> $ Rgt"); $ this-> db-> query ("update '". $ this-> tablefix. "catagory 'set' Rgt '= 'rgt' + 2 WHERE 'rgt '>=$ Rgt ");} // INSERT if ($ this-> db-> query ("insert '". $ this-> tablefix. "catagory 'set 'lft '=' $ Rgt ', 'rgt' = '$ Rgt' + 1, 'name' = '$ sortname '")) {// $ this-> referto ("a new category is successfully added", "JAVASCRIPT: HISTORY. BACK (1) ", 3); return 1;} else {// $ this-> referto (" failed to add a new category "," JAVASCRIPT: HISTORY. BACK (1) ", 3); return-1 ;}// end func/*** Short description. * delete Category * Detail description * @ param none * @ global none * @ since 1.0 * @ access private * @ return void * @ update date time */function deletesort ($ CatagoryID) {// Obtain the left and right values of the deleted category and check whether any subclass exists. If yes, delete $ Result = $ this-> checkcatagory ($ CatagoryID) together ); $ Lft = $ Result ['lft ']; $ Rgt = $ Result ['rgt']; // execute the DELETE if ($ this-> db-> query ("delete from '". $ this-> tablefix. "catagory 'Where 'lft '>=$ Lft AND 'rgt' <= $ Rgt") {$ Value = $ Rgt-$ Lft + 1; // UPDATE the left and right values $ this-> db-> query ("update '". $ this-> tablefix. "catagory 'set 'lft '= 'lft'-$ Value WHERE 'lft '> $ Lft"); $ this-> db-> query ("update '". $ this-> tablefix. "catagory 'set' Rgt '= 'rgt'-$ Value WHERE 'rgt '> $ Rgt"); // $ this-> referto ("successfully deleted category ", "javascript: history. back (1) ", 3); return 1;} else {// $ this-> referto (" failed to delete Category "," javascript: history. back (1) ", 3); return-1 ;}// end func/*** Short description. * 1. all child classes, excluding themselves; 2 including all child classes; 3 not including all parent classes; 4; contains all of your parent classes * Detail description * @ param none * @ global none * @ since 1.0 * @ access private * @ return void * @ update date time */function getcatagory ($ CatagoryID, $ type = 1) {$ Result = $ this-> checkcatagory ($ CatagoryID); $ Lft = $ Result ['lft ']; $ Rgt = $ Result ['rgt ']; $ SeekSQL = "SELECT * from '". $ this-> tablefix. "catagory 'Where"; switch ($ type) {case "1": $ condition = "'lft '> $ Lft AND 'rgt' <$ Rgt"; break; case "2": $ condition = "'lft '>=$ Lft AND 'rgt' <= $ Rgt"; break; case "3 ": $ condition = "'lft '<$ Lft AND 'rgt'> $ Rgt"; break; case "4 ": $ condition = "'lft '<= $ Lft AND 'rgt'> = $ Rgt"; break; default: $ condition = "'lft '> $ Lft AND 'rgt' <$ Rgt" ;;}$ SeekSQL. = $ condition. "order by 'lft 'asc"; $ Sorts = $ this-> db-> getrows ($ SeekSQL); return $ Sorts ;} // end func/*** Short description. * obtain the parent class * Detail description * @ param none * @ global none * @ since 1.0 * @ access private * @ return void * @ update date time */function getparent ($ CatagoryID) {$ Parent = $ this-> getcatagory ($ CatagoryID, 3); return $ Parent;} // end func/*** Short description. * Mobile class, move * Detail description * @ param none * @ global none * @ since 1.0 * @ access private * @ return void * @ update date time */function movecatagory ($ SelfCatagoryID, $ ParentCatagoryID) {$ SelfCatagory = $ this-> checkcatagory ($ SelfCatagoryID); $ NewCatagory = $ this-> checkcatagory ($ ParentCatagoryID ); $ SelfLft = $ SelfCatagory ['lft ']; $ SelfRgt = $ SelfCatagory ['rgt']; $ Value = $ SelfRgt-$ SelfLft; // Obtain the IDs of all categories to update the left and right values. $ CatagoryIDS = $ this-> getcatagory ($ SelfCatagoryID, 2); foreach ($ CatagoryIDS as $ v) {$ IDS [] = $ v ['catagoryid'];} $ InIDS = implode (",", $ IDS); $ ParentLft = $ NewCatagory ['lft ']; $ ParentRgt = $ NewCatagory ['rgt ']; // print_r ($ InIDS); // print_r ($ NewCatagory); // print_r ($ SelfCatagory); // exit; if ($ ParentRgt> $ SelfRgt) {$ UpdateLeftSQL = "update '". $ this-> tablefix. "catagory 'set 'lft '= 'lft'-$ Value-1 WHERE 'lft '> $ SelfRgt AND 'rgt' <= $ ParentRgt "; $ UpdateRightSQL = "update '". $ this-> tablefix. "catagory 'set' Rgt '= 'rgt'-$ Value-1 WHERE 'rgt '> $ SelfRgt AND 'rgt' <$ ParentRgt "; $ TmpValue = $ ParentRgt-$ SelfRgt-1; $ UpdateSelfSQL = "updat '". $ this-> tablefix. "catagory 'set 'lft '= 'lft' + $ TmpValue, 'rgt '= 'rgt' + $ TmpValue WHERE 'catagoryid' IN ($ InIDS )";} else {$ UpdateLeftSQL = "update '". $ this-> tablefix. "catagory 'set 'lft '= 'lft' + $ Value + 1 WHERE 'lft '> $ ParentRgt AND 'lft' <$ SelfLft"; $ UpdateRightSQL = "update '". $ this-> tablefix. "catagory 'set' Rgt '= 'rgt' + $ Value + 1 WHERE 'rgt '> = $ ParentRgt AND 'rgt' <$ SelfLft "; $ TmpValue = $ SelfLft-$ ParentRgt; $ UpdateSelfSQL = "updat '". $ this-> tablefix. "catagory 'set 'lft '= 'lft'-$ TmpValue, 'rgt '= 'rgt'-$ TmpValue WHERE 'catagoryid' IN ($ InIDS )";} $ this-> db-> query ($ UpdateLeftSQL); $ this-> db-> query ($ UpdateRightSQL); $ this-> db-> query ($ UpdateSelfSQL ); // $ this-> referto ("successfully moved Category", "javascript: history. back (1) ", 3); return 1;} // end func/*** Short description. ** Detail description * @ param none * @ global none * @ since 1.0 * @ access private * @ return void * @ update date time */function checkcatagory ($ CatagoryID) {// check whether the parent class ID exists $ SQL = "SELECT * FROM '". $ this-> tablefix. "catagory 'where' catagoryid' = '$ CatagoryID 'limit 1"; $ Result = $ this-> db-> getrow ($ SQL); if (count ($ Result) <1) {$ this-> referto ("the parent class ID does not exist. check", "javascript: history. back (1) ", 3);} return $ Result;} // end func/*** Short description. ** Detail description * @ param none * @ global none * @ since 1.0 * @ access private * @ return array ($ Catagoryarray, $ Deep) * @ update date time */function sort2array ($ CatagoryID = 0) {$ Output = array (); if ($ CatagoryID = 0) {$ CatagoryID = $ this-> getrootid () ;}if (empty ($ CatagoryID) {return array (); exit ;} $ Result = $ this-> db-> query ('select Lft, Rgt FROM ''. $ this-> tablefix. 'catagory 'where' catagoryid' = '. $ CatagoryID); if ($ Row = $ this-> db-> fetch_array ($ Result) {$ Right = array (); $ Query = 'select * FROM ''. $ this-> tablefix. 'catagory 'Where Lft '. $ Row ['lft ']. AND '. $ Row ['rgt ']. 'order BY Lft ASC '; $ Result = $ this-> db-> query ($ Query ); while ($ Row = $ this-> db-> fetch_array ($ Result) {if (count ($ Right)> 0) {while ($ Right [count ($ Right) -1] <$ Row ['rgt ']) {array_pop ($ Right) ;}}$ Output [] = array ('sort' => $ Row, 'deep '=> count ($ Right); $ Right [] = $ Row ['rgt'] ;}} return $ Output ;} // end func/*** Short description. ** Detail description * @ param none * @ global none * @ since 1.0 * @ access private * @ return void * @ update date time */function getrootid () {$ Query = "SELECT * FROM '". $ this-> tablefix. "catagory 'Order BY 'lft 'asc limit 1"; $ RootID = $ this-> db-> getrow ($ Query); if (count ($ RootID)> 0) {return $ RootID ['catagoryid'];} else {return 0 ;}// end func/*** Short description. ** Detail description * @ param none * @ global none * @ since 1.0 * @ access private * @ return void * @ update date time */function referto ($ msg, $ url, $ sec) {echo"
 "; Echo"
 "; If (is_array ($ msg) {foreach ($ msg as $ key => $ value) {echo $ key." => ". $ value ."
";}} Else {echo $ msg;} exit;} // end func} // end class?>

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.