The php + mysql database implements an infinite classification method, mysql database. The php + mysql database implements an unlimited classification method. This article describes how to implement an unlimited classification for the php + mysql database. Share it with you for your reference. The specific analysis is as follows:
This article describes how to implement unlimited classification in the php + mysql database. Share it with you for your reference. The specific analysis is as follows:
This php unlimited classification code is complete, including the functions of adding, deleting, editing, and moving databases to mysql. It also provides the Database SQL table structure. the code is as follows:
The code is as follows:
// Connect to the database
$ Link = mysql_connect ('localhost', 'root', '') or die (mysql_error ());
Mysql_select_db ('class', $ link) or die (mysql_error ());
Mysql_query ("set names gbk ");
// Unlimited class library
Class sortclass {
Var $ data = array ();
Var $ child = array (-1 => array ());
Var $ layer = array (-1 =>-1 );
Var $ parent = array ();
Var $ link;
Var $ table;
Function sortclass ($ link, $ table ){
$ This-> setnode (0,-1, 'Top node ');
$ This-> link = $ link;
$ This-> table = $ table;
$ Node = array ();
$ Results = mysql_query ("select * from $ this-> table", $ this-> link );
While ($ node = mysql_fetch_array ($ results )){
$ This-> setnode ($ node ['id'], $ node ['F _ id'], $ node ['name']);
}
}
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;
$ This-> layer [$ id] =! Isset ($ this-> layer [$ parent])? 0: $ this-> layer [$ parent] + 1;
}
Function getlist (& $ tree, $ root = 0 ){
Foreach ($ this-> child [$ root] as $ key => $ id ){
$ Tree [] = $ id;
If ($ this-> child [$ id]) $ this-> getlist ($ tree, $ id );
}
}
Function getvalue ($ id) {return $ this-> data [$ id];}
Function getlayer ($ id, $ space = false ){
Return $ space? Str_repeat ($ space, $ this-> layer [$ id]): $ this-> layer [$ id];
}
Function getparent ($ id) {return $ this-> parent [$ id];}
Function getparents ($ id ){
While ($ this-> parent [$ id]! =-1 ){
$ Id = $ parent [$ this-> layer [$ id] = $ this-> parent [$ id];
}
Ksort ($ parent );
Reset ($ parent );
Return $ parent;
}
Function getchild ($ id) {return $ this-> child [$ id];}
Function getchilds ($ id = 0 ){
$ Child = array ($ id );
$ This-> getlist ($ child, $ id );
Return $ child;
}
Function addnode ($ name, $ pid ){
// Echo "insert into $ this-> table ('F _ id', 'name') values ('$ pid', '$ name')"; exit;
Mysql_query ("insert into $ this-> table ('F _ id', 'name') values ('$ pid', '$ name ')", $ this-> link );
}
Function modnode ($ cid, $ newname ){
Mysql_query ("update $ this-> table set 'name' = '$ newname' where 'id' = $ cid", $ this-> link );
}
Function delnode ($ cid ){
$ Allchilds = $ this-> getchilds ($ cid );
$ SQL = '';
If (emptyempty ($ allchilds )){
$ SQL = "delete from $ this-> table where 'id' = $ cid ";
} Else {
$ SQL = 'delete from '. $ this-> table. 'Where' ID' in ('. implode (',', $ allchilds ). ','. $ cid. ')';
}
Mysql_query ($ SQL, $ this-> link );
}
Function movenode ($ cid, $ topid ){
Mysql_query ("update $ this-> table set 'F _ id' = $ topid where 'id' = $ cid", $ this-> link );
}
}
// Function
Function back (){
Echo'