Unlimited Classification Based on THINKPHP3.0

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn about infinitus classification, which is often used in projects. There are also various implementation methods for infinitus classification, next I will share a thinkphp3.0-based unlimited classification, and also supports general value-based unlimited classification. This class was found on the Internet a long time ago. The original author did not have any information. Here I just made some modifications and formatting on the original basis.
The classification table must contain three basic fields: cid, fid, and name: Category cid, parent fid, and category name.
Table Structure: CREATE TABLE `think_category` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`fid` int(11) DEFAULT NULL,
`name` varchar(30) DEFAULT NULL
PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
You can use either of the following methods:
The first method is based on THINKPHP3.0. After downloading, put Category. class. php In the ORG directory of the current project (other directories can be referenced as long as they can be normally used) Import ("@. ORG. Category ");
$ Cat = new Category ('category ', array ('cid', 'fid', 'name', 'fullname '));
$ S = $ cat-> getList (); // get the category structure
$ S = $ cat-> getList ('', 1); // obtain the subcategory structure of fid = 1
$ S = $ cat-> getList ($ condition, 1); // $ condition is the query condition and obtains the subcategory structure of fid = 1.
$ S = $ cat-> getPath (3); // obtain the path of category id = 3
$ Data = array ("fid" => 0, "name" => "new category name ");
$ S = $ cat-> add ($ data); // add a category. $ data must contain the fid of the parent category.
$ Data = array ("cid" => 2, "name" => "modified category name ");
$ S = $ cat-> edit ($ data); // modify the category. $ data must contain the category ID.
$ S = $ cat-> del (10); // delete a category id = 10
The second method does not require the support of TP, but the data structure must be the same.

Category file Category. class. php content:
/**
+ ------------------------------------------------------------------------------
* Category Management
+ ------------------------------------------------------------------------------
*/
Class Category {

Private $ model; // data table model of classification
Private $ rawList = array (); // original category data
Private $ formatList = array (); // formatted category
Private $ error = ""; // error message
Private $ icon = array ('│', 'hangzhou', 'hangzhou'); // formatted characters
Private $ fields = array (); // field ing, category id, parent category fid, category name, formatted category name fullname

/**
+ ----------------------------------------------------------
* Constructor, object initialization
+ ----------------------------------------------------------
* @ Param array: the object $ model array or object. The model name of the data table based on TP3.0. If TP is not used, null values can be passed.
* @ Param array $ field ing, category cid, parent category fid, category name, formatted category name fullname
+ ----------------------------------------------------------
*/

Public function _ construct ($ model = '', $ fields = array ()){
If (is_string ($ model )&&(! Empty ($ model ))){
If (! $ This-> model = D ($ model ))
$ This-> error = $ model. "The model does not exist! ";
}
If (is_object ($ model ))
$ This-> model = & $ model;

$ This-> fields ['cid'] = $ fields ['0']? $ Fields ['0']: 'cid ';
$ This-> fields ['fid'] = $ fields ['1']? $ Fields ['1']: 'fid ';
$ This-> fields ['name'] = $ fields ['2']? $ Fields ['2']: 'name ';
$ This-> fields ['fullname'] = $ fields ['3']? $ Fields ['3']: 'fullname ';
}

/**
+ ----------------------------------------------------------
* Retrieve category information data
+ ----------------------------------------------------------
* @ Param array, string $ condition query condition
* @ Param string $ orderby sorting
+ ----------------------------------------------------------
*/
Private function _ findAllCat ($ condition, $ orderby = NULL ){
$ This-> rawList = empty ($ orderby )? $ This-> model-> where ($ condition)-> select (): $ this-> model-> where ($ condition)-> order ($ orderby) -> select ();
}

/**
+ ----------------------------------------------------------
* Returns all the same level-1 subcategories of the given upper-level category $ fid.
+ ----------------------------------------------------------
* @ Param int $ fid: input the fid to be queried.
+ ----------------------------------------------------------
* @ Return array returns the structure information.
+ ----------------------------------------------------------
*/
Public function getChild ($ fid ){
$ Childs = array ();
Foreach ($ this-> rawList as $ Category ){
If ($ Category [$ this-> fields ['fid'] = $ fid)
$ Childs [] = $ Category;
}
Return $ childs;
}

/**
+ ----------------------------------------------------------
* Recursively format characters before classification
+ ----------------------------------------------------------
* @ Param int $ cid classification cid
* @ Param string $ space
+ ----------------------------------------------------------
*/
Private function _ searchList ($ cid = 0, $ space = ""){
$ Childs = $ this-> getChild ($ cid );
// An array of lower-level categories
// End recursion if no subcategory exists
If (! ($ N = count ($ childs )))
Return;
$ M = 1;
// Loop all subcategories
For ($ I = 0; $ I <$ n; $ I ++ ){
$ Pre = "";
$ Pad = "";
If ($ n = $ m ){
$ Pre = $ this-> icon [2];
} Else {
$ Pre = $ this-> icon [1];
$ Pad = $ space? $ This-> icon [0]: "";
}
$ Childs [$ I] [$ this-> fields ['fullname'] = ($ space? $ Space. $ pre: ""). $ childs [$ I] [$ this-> fields ['name'];
$ This-> formatList [] = $ childs [$ I];
$ This-> _ searchList ($ childs [$ I] [$ this-> fields ['cid'], $ space. $ pad. ""); // recursive next-level classification
$ M ++;
}
}

/**
+ ----------------------------------------------------------
* If the data model is not used, you can transmit data from the outside to obtain recursive formatting and classification.
+ ----------------------------------------------------------
* @ Param array, string $ condition
* @ Param int $ cid start category
* @ Param string $ orderby sorting
+ ----------------------------------------------------------
* @ Return array returns the structure information.
+ ----------------------------------------------------------
*/
Public function getList ($ condition = NULL, $ cid = 0, $ orderby = NULL ){
Unset ($ this-> rawList, $ this-> formatList );
$ This-> _ findAllCat ($ condition, $ orderby, $ orderby );
$ This-> _ searchList ($ cid );
Return $ this-> formatList;
}

/**
+ ----------------------------------------------------------
* Get Structure
+ ----------------------------------------------------------
* @ Param array $ data two-dimensional array data
* @ Param int $ cid start category
+ ----------------------------------------------------------
* @ Return array recursively formatted the category array
+ ----------------------------------------------------------
*/
Public function getTree ($ data, $ cid = 0 ){
Unset ($ this-> rawList, $ this-> formatList );
$ This-> rawList = $ data;
$ This-> _ searchList ($ cid );
Return $ this-> formatList;
}

/**
+ ----------------------------------------------------------
* Get error information
+ ----------------------------------------------------------
* @ Return string error message string
+ ----------------------------------------------------------
*/
Public function getError (){
Return $ this-> error;
}

/**
+ ----------------------------------------------------------
* Check whether the classification parameter $ cid is null.
+ ----------------------------------------------------------
* @ Param int $ cid start category
+ ----------------------------------------------------------
* @ Return boolean recursively format the category Array
+ ----------------------------------------------------------
*/
Private function _ checkCatID ($ cid ){
If (intval ($ cid )){
Return true;
} Else {
$ This-> error = "the parameter category ID is null or invalid! ";
Return false;
}
}

/**
+ ----------------------------------------------------------
* Check whether the classification parameter $ cid is null.
+ ----------------------------------------------------------
* @ Param int $ cid classification cid
+ ----------------------------------------------------------
*/
Private function _ searchPath ($ cid ){
// Check parameters
If (! $ This-> _ checkCatID ($ cid ))
Return false;
$ Rs = $ this-> model-> find ($ cid); // initialize the object and find the parent Id;
$ This-> formatList [] = $ rs; // Save the result
$ This-> _ searchPath ($ rs [$ this-> fields ['fid']);
}

/**
+ ----------------------------------------------------------
* Query the path of a specified cid.
+ ----------------------------------------------------------
* @ Param int $ cid classification cid
+ ----------------------------------------------------------
* @ Return array
+ ----------------------------------------------------------
*/
Public function getPath ($ cid ){
Unset ($ this-> rawList, $ this-> formatList );
$ This-> _ searchPath ($ cid); // query the category path
Return array_reverse ($ this-> formatList );
}

/**
+ ----------------------------------------------------------
* Add category
+ ----------------------------------------------------------
* @ Param array $ a one-dimensional data array. The data to be added must contain the ID of the upper-level category.
+ ----------------------------------------------------------
* @ Return boolean: The value is successfully added. The corresponding category ID is returned. If the value fails to be added, FALSE is returned;
+ ----------------------------------------------------------
*/
Public function add ($ data ){
If (empty ($ data ))
Return false;
Return $ this-> model-> data ($ data)-> add ();
}

/**
+ ----------------------------------------------------------
* Modify a category
+ ----------------------------------------------------------
* @ Param array $ a one-dimensional data array. $ data must contain the cid of the category to be modified.
+ ----------------------------------------------------------
* @ Return: the boolean Group is successfully modified. The corresponding category ID is returned. If the modification fails, FALSE is returned;
+ ----------------------------------------------------------
*/
Public function edit ($ data ){
If (empty ($ data ))
Return false;
Return $ this-> model-> data ($ data)-> save ();
}

/**
+ ----------------------------------------------------------
* Delete A category
+ ----------------------------------------------------------
* @ Param int $ cid classification cid
+ ----------------------------------------------------------
* @ Return boolean: The operation is successful. The corresponding category ID is returned. If the deletion fails, FALSE is returned.
+ ----------------------------------------------------------
*/
Public function del ($ cid ){
$ Cid = intval ($ cid );
If (empty ($ cid ))
Return false;
$ Conditon [$ this-> fields ['cid'] = $ cid;
Return $ this-> model-> where ($ conditon)-> delete ();
}

}
?>
This editor is too bad, the edit box is so small, detailed use and test package to my blog download it, http://blog.51edm.org/post/78

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.