Php unlimited classification-PHP source code

Source: Internet
Author: User
Tags mysql tutorial
Ec (2); Create Database tutorials and tables: CREATEDATABASE 'sortclass' ultultcharsetutf8; CREATETABLEIFNOTEXISTS 'class' ('cid' mediumint (8) values, 'pid' mediumint (8) unsignedNOTNULL, script ec (2); script

Create Database tutorials and tables:

Create database 'sortclass' default charset utf8;
Create table if not exists 'class '(
'Cid' mediumint (8) unsigned not null auto_increment,
'Pid 'mediumint (8) unsigned not null,
'Cname' varchar (50) not null,
Primary key ('cid '),
KEY 'pid '('pid ')
) ENGINE = MyISAM default charset = utf8;

File Processing example:

Header ("Content-type: text/html; charset = UTF-8 ");
// Connect to the database
$ Link = mysql tutorial _ connect ('localhost', 'root', 'Eric ') or die (mysql_error ());
Mysql_select_db ('sortclass', $ link );
// 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_assoc ($ results )){
$ This-> setNode ($ node ['cid'], $ node ['pid'], $ node ['cname']);
}
}
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 ){
Mysql_query ("insert into $ this-> table ('pid ', 'cname') values (' $ pid ',' $ name')", $ this-> link );
}
Function modNode ($ cid, $ newName ){
Mysql_query ("update $ this-> table set 'cname' = '$ newname' where 'cid' = $ cid", $ this-> link );
}
Function delNode ($ cid ){
$ AllChilds = $ this-> getChilds ($ cid );
$ SQL = '';
If (empty ($ allChilds )){
$ SQL = "delete from $ this-> table where 'cid' = $ cid ";
} Else {
$ SQL = 'delete from '. $ this-> table. 'where'cid' in ('. implode (',', $ allChilds ). ','. $ cid. ')';
}
Mysql_query ($ SQL, $ this-> link );
}
Function moveNode ($ cid, $ topid ){
Mysql_query ("update $ this-> table set 'pid' = $ topid where 'cid' = $ cid", $ this-> link );
}
}
// Function
Function back (){
Echo'

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.