PHP unlimited classification class. Copy the code as follows :? Php *** @ authorYangHuan * @ datetime * @ version1.0.0 *** invalid description. ** Detaildescription * @ author * @ version1.0 * @ copyright * @ acce
The code is as follows:
/**
* @ Author YangHuan
* @ Datetime
* @ Version 1.0.0
*/
/**
* Short description.
*
* Detail description
* @ Author
* @ Version 1.0
* @ Copyright
* @ Access public
*/
Class Tree
{
/**
* Description
* @ Var
* @ Since 1.0
* @ Access private
*/
Var $ data = array ();
/**
* Description
* @ Var
* @ Since 1.0
* @ Access private
*/
Var $ child = array (-1 => array ());
/**
* Description
* @ Var
* @ Since 1.0
* @ Access private
*/
Var $ layer = array (-1 =>-1 );
/**
* Description
* @ Var
* @ Since 1.0
* @ Access private
*/
Var $ parent = array ();
/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function Tree ($ value)
{
$ This-> setNode (0,-1, $ value );
} // End func
/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function setNode ($ id, $ parent, $ value)
{
$ Parent = $ parent? $ Parent: 0;
$ This-> data [$ id] = $ value;
$ This-> child [$ id] = array ();
$ This-> child [$ parent] [] = $ id;
$ This-> parent [$ id] = $ parent;
If (! Isset ($ this-> layer [$ parent])
{
$ This-> layer [$ id] = 0;
}
Else
{
$ This-> layer [$ id] = $ this-> layer [$ parent] + 1;
}
} // End func
/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getList (& $ tree, $ root = 0)
{
Foreach ($ this-> child [$ root] as $ key => $ id)
{
$ Tree [] = $ id;
If ($ this-> child [$ id]) $ this-> getList ($ tree, $ id );
}
} // End func
/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getValue ($ id)
{
Return $ this-> data [$ id];
} // End func
/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getLayer ($ id, $ space = false)
{
Return $ space? Str_repeat ($ space, $ this-> layer [$ id]): $ this-> layer [$ id];
} // End func
/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getParent ($ id)
{
Return $ this-> parent [$ id];
} // End func
/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getParents ($ id)
{
While ($ this-> parent [$ id]! =-1)
{
$ Id = $ parent [$ this-> layer [$ id] = $ this-> parent [$ id];
}
Ksort ($ parent );
Reset ($ parent );
Return $ parent;
} // End func
/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getChild ($ id)
{
Return $ this-> child [$ id];
} // End func
/**
* Short description.
*
* Detail description
* @ Param none
* @ Global none
* @ Since 1.0
* @ Access private
* @ Return void
* @ Update date time
*/
Function getChilds ($ id = 0)
{
$ Child = array ($ id );
$ This-> getList ($ child, $ id );
Return $ child;
} // End func
} // End class
?>
Usage
PHP code:
The code is as follows:
// New Tree (root directory name );
// The ID of the root directory is automatically allocated to 0.
$ Tree = new Tree ('root directory ');
// SetNode (Directory ID, parent ID, directory name );
$ Tree-> setNode (1, 0, 'directory 1 ');
$ Tree-> setNode (2, 0, 'directory 2 ');
$ Tree-> setNode (3, 0, 'directory 3 ');
$ Tree-> setNode (4, 3, 'directory 100 ');
$ Tree-> setNode (5, 3, 'directory 100 ');
$ Tree-> setNode (6, 3, 'directory 100 ');
$ Tree-> setNode (7, 2, 'directory 100 ');
$ Tree-> setNode (8, 2, 'directory 100 ');
$ Tree-> setNode (9, 2, 'directory 100 ');
$ Tree-> setNode (10, 6, 'directory 3.3.1 ');
$ Tree-> setNode (11, 6, 'directory 3.3.2 ');
$ Tree-> setNode (12, 6, 'directory 3.3.3 ');
// GetChilds (specify the Directory ID );
// Obtain the sub-directory of the specified directory. If no sub-directory is specified, it starts from the root directory.
$ Category = $ Tree-> getChilds ();
// Print the output
Foreach ($ category as $ key => $ id)
{
Echo $ Tree-> getLayer ($ id, '|-'). $ Tree-> getValue ($ id )."
\ N ";
}
PHP unlimited classification-bkJia code
The code is as follows:
// Unlimited category. find all parent classes from subclass
// $ Id subclass ID
Function php100_xd ($ id ){
$ SQL = "select * from fl where id = '$ ID '";
$ Q = mysql_query ($ SQL );
$ Rs = mysql_fetch_array ($ q );
$ Rs ['fid'] = 0? "": Fl ($ rs ['fid']);
Echo $ rs ['name']. "-";
}
// Read sub-classes under all parent classes
// $ F What does top-level classification start from? $ s style
Function php100_dx ($ f = 0, $ s = ""){
$ SQL = "select * from fl where fid = $ f ";
$ Q = mysql_query ($ SQL );
$ S = $ s ."-";
While ($ rs = mysql_fetch_array ($ q )){
Echo"
$ S ". $ rs ['name'];
Flt ($ rs ['id'], $ s );
}
}
The http://www.bkjia.com/PHPjc/317685.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/317685.htmlTechArticle code is as follows :? Php/*** @ author YangHuan * @ datetime * @ version 1.0.0 * // *** Short description. ** Detail description * @ author * @ version 1.0 * @ copyright * @ acce...