Php unlimited classification program code _ PHP Tutorial

Source: Internet
Author: User
The code of the infinitely classified program in php. The article is an original article about the unlimited classification implemented in php. if you need to know it, you can check it out. Unlimited classification is like creating a folder in windows. the new folder article is an original article about the unlimited classification implemented in php. if you need to know about it, you can see it.

Unlimited classification is like creating a folder in windows. you can create a folder in the new folder, so that there is an infinite loop. The same is true for the infinite classification. the parent class can be divided into its sub-classes, sub-classes can also be divided into its sub-classes, so that the infinite loop continues.

The code is as follows:

--
-- Database: 'tree'
--
Create database 'tree' default character set gb2312 COLLATE gb2312_chinese_ci;
USE 'Tree ';


--
-- Table structure 'class'
--

Create table 'class '(
'Id' int (11) not null auto_increment,
'Name' varchar (10) not null,
'Pid 'int (11) not null,
'Dest' varchar (100) default '0 ',
Primary key ('id ')
) ENGINE = MyISAM default charset = gb2312 AUTO_INCREMENT = 30;


$ Link = mysql_connect ('localhost', 'root', 'root ');
Mysql_select_db ('tree ');
Mysql_query ('set names gbk ');
If ($ _ GET ['act '] = 'ADD '){
$ Name = $ _ POST ['name'];
If ($ name = '') exit ('name not Null ');
$ Pid = $ _ POST ['pid'];
If ($ pid! = 0 ){
$ SQL = "select * from class where id =". $ pid;
$ Result = mysql_query ($ SQL );
$ Row = mysql_fetch_array ($ result );
$ Depth = $ row ['dest']. ','. mysql_insert_id (); // $ getID indicates the ID of the last record.
} Else {
$ Depth = 0;
}
$ SQL = "INSERT INTO class (name, pid, depth) VALUES ('". $ name. "','". $ pid. "','". $ depth. "')";
$ Result = mysql_query ($ SQL );
If (! $ Result ){
Exit ("shibai $ SQL ");
} Else {
Exit ("chenggong ");
}
}
?>








// $ SQL = "select * from 'class' order by sort asc, id Desc ";
// $ SQL = "select * from class where pid = 0 ";
// $ Result = mysql_query ($ SQL );
// While ($ row = mysql_fetch_array ($ result )){
// $ Class_arr [] = array ($ row ['id'], $ row ['name'], $ row ['classid '], $ row ['sort ']);
// Echo $ row ['name']. $ row ['id']. '---'. $ row ['pid '].'
';
Sorttree (0 );


//}

Function sorttree ($ id ){
// If ()
$ SQL = "select * from class where pid =". $ id;
$ Result = mysql_query ($ SQL );
// $ A = mysql_fetch_array ($ result );
// $ Row = mysql_fetch_array ($ result );
While ($ row = mysql_fetch_array ($ result )){
$ Rid = explode (",", $ row ['depth ']);
$ I = count ($ rid );
$ N = str_pad ('', $ I, '-', STR_PAD_RIGHT );
$ N = str_replace ("-", "", $ n );
// Print_r ($ row ).'
';
If ($ row ['pid '] = 0 ){
Echo $ n. '| -----'. $ row ['name']. '----- |
';
} Else {
Echo $ n. '|-'. $ row ['name'].'
';
}
Sorttree ($ row ['id']);
}
}

Function sort_s ($ id ){
// If ()
$ SQL = "select * from class where pid =". $ id;
$ Result = mysql_query ($ SQL );
// $ A = mysql_fetch_array ($ result );
// $ Row = mysql_fetch_array ($ result );
While ($ row = mysql_fetch_array ($ result )){
$ Rid = explode (",", $ row ['depth ']);
$ I = count ($ rid );
$ N = str_pad ('', $ I, '-', STR_PAD_RIGHT );
$ N = str_replace ("-", "", $ n );
// Print_r ($ row ).'
';
If ($ row ['pid '] = 0 ){
Echo"". $ N. '| ------'. $ row ['name']." ----- |Rn ";
} Else {
Echo"". $ N. '|-'. $ row ['name']."Rn ";
}
Sort_s ($ row ['id']);
}
}
?>

Principles

How can we list all its categories one by one?
First, let's assume there is such a three-level classification: news> PHP News> PHP6.0.
If you want to find the news "PHP6.0 has come out", click the news first, and then click the PHP News.

We can find out, that is, we can look down through the grandfather class level-1, in turn we just

If you know the parent class of a subclass, you can find it. In this way, we can design multiple databases.

You can use the field of a parent class id to implement the unlimited classification function.

Bytes. Unlimited classification is like creating a folder in windows...

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.