PHP unlimited classification program code

Source: Internet
Author: User

The infinite classification method may be used in most days for data classification. We may have various subclasses for news or software channels, but we do not know these subclasses, so we have to use the infinite classification method, next I will introduce it to you.

First category (parent category)-> second category (sub-category)-> third category (Sun category)

The more kinship classification, the more complex and difficult it is to control programs and databases. classification processing and control at the same level is very simple, because only one database is required to record the classification at this level, such as system and news, processing at this level is very simple, but for a website, level-1 classification is not enough, and further classification is required, such:

System> linux and windows
News-> linux news, windows news

In this way, the classification is clearer, at least understandable. The system includes linux and windows, and the news includes linux news and windows news. In order to make the information clearer, the classification should continue:

Linux-> System Tools, kernels, programming languages, and development tools
...

When classification reaches Level 3, information and materials are processed more clearly. That is to say, the more detailed the classification is, the more convenient it is, this facilitates information processing and allows users to clearly find the required information. However, with the continuous refinement of classification, it becomes increasingly difficult to control programs and databases.

Difficulty 1: how to deal with these interrelated kinship classifications in the database?
Difficulty 2: How to Use php to achieve this clear relationship?

This multi-classification and fine-grained classification is a problem that every php programmer must solve. It is inevitable to create a good and outstanding website classification problem, the solution to this problem is quite complicated. The biggest problem is the classification of the database. If the database is improperly processed, it will bring about a huge workload or even have to re-plan the database...

This is not an exaggeration, because many people will use the first-level classification to create a database in database processing. At that time, I also used this method to process classification, because most websites are classified to the third-level, therefore, the data warehouse only needs three classified databases for processing. however, when downward classification is required, the disadvantages of this approach are revealed, because the more you divide, the more workload, the larger the program volume ..

The method I want to introduce is how to use a classification database to create an infinitely downward classification method. readers who have used windows know that windows folders can create an infinitely hierarchical directory, you can continue to create a directory under the directory, so that you can continue to split it. This function is also available in Linux Directory creation. The method I introduced is the same as this method.

Database Planning
As we talked about the complexity of classification, it is very important to plan a database to implement unlimited classification.

I have introduced the Database Planning of the Forum. Good news is that the Forum can achieve unlimited follow-up, and unlimited classification is an extension of this form. Classification is also a parent association, therefore, the classification database is how to establish the parent relationship clearly. There are several difficulties.

1) how to store information of various categories;
2) How to Deal with classification kinship;
3) How to query information;

The relational database processing is similar to the Forum database processing. Here, a type database is created to process classification:

Create field:
Id (int): used to record the natural sequence numbers of each category
Uid (int): id of the parent category used to record the category
Type (char): name of a category
Roue_id (varchar): kinship tree, with the id connection: 0: 2: 10: 20: indicates the parent-Source Relationship
Roue_char (varchar): kinship tree, similar to: System: linux: Development Tool: gcc: (This field does not matter, in order to better understand the kinship, of course, the character expression is more direct than the number expression ^ o ^, but it is best to add this field)

In this way, an infinitely classified Category Table is created. Next, you need to create a database for storing information. It is most convenient to process and query a table. Therefore, a table Storage information type_message is created here:

Id (int): the serial number of the information;
Typeid (int): id of the category;
Title (varchar): Information title;
Message (text): information content;
Time: the time when the information was created;

The two data tables can complete the infinite classification task (the auxiliary fields of the two tables are not added, and readers can add them themselves ).

All the remaining tasks are handled by php.

Program Control
This step is the most complex and difficult to implement the unlimited classification function. First, let's look at the steps that need to be completed by the Program:

1) create a category for upload;
2) create Information upload;
3) clearly show the relationship between each category and its relationship;
4) query processing function;
5) how to handle the editing and deletion functions;

The fifth step is the most difficult of the five steps, because the editing and deletion of categories involves a one-to-one problem.

Next I will describe php program control one by one:

1) Create category upload

Before introducing this function, we will first introduce the explode () function, which is a string processing function used to break down strings. The specific usage is as follows:

Break down the numbers in "0: 1: 2: 3: 4"

 

$ Val = "0: 1: 2: 3: 4 ";
$ Rid = explode (":", $ val );
 

After processing by the explode () function, all the numbers in $ val are decomposed into the $ rid array. to reference them, print the echo "$ rid [0], $ rid [1], $ rid [2]… "; The. explode () function plays a very important role in the entire classification process. Now we will introduce program control for non-existing classification.

Assume that a total category is 0, and all categories are its descendant categories. Now we will create the first classification "system" to see its storage format in the database:

Id | uid | type | rout_id | rout_char
1 | 0 | system | 0: 1 | System

Next we will divide it into Linux ":

Id | uid | type | rout_id | rout_char
2 | 1 | Linux | system Linux

The above is the form of database storage. Now we can complete the php code, which is very similar to the Forum code. All we need to do is to put the classification id into the uid, the uid of the parent category is 0. Let's look at the code below:

 

The Code is as follows: Copy code

 

<?
.....
.....

// Set the hosts page
If (empty ($ func) $ func = "showtype ";

// Set the uid of the parent category
If (empty ($ uid) $ uid = 0;

// Database storage ************************************ ************
If ($ func = "save "):

$ Fields = "";
$ Values = "";

If ($ id! = ""){
$ Fields. = ", id ";
$ Values. = ", $ id ";
}

If ($ uid! = ""){
$ Fields. = ", uid ";
$ Values. = ", $ uid ";
}

If ($ type! = ""){
$ Fields. = ", type ";
$ Values. = "," $ type "";
}

If ($ route_id = ""){

// Obtain the route_id of the parent category
If ($ uid! = 0 ){
$ Result = mysqlquery ("select * from type where id = $ uid ");
$ Route_id = mysql_result ($ result, 0, "route_id ");
} Else {
$ Routr_id = "0 ";
}
$ Fields. = ", route_id ";
// Form your own route_id
$ Route_id = "$ route_id: $ id ";
$ Values. = "," $ route_id "";
}

// Form your own route_char
If ($ route_char! = ""){
$ Fields. = ", route_char ";
$ Route_char = "$ route_char: $ type ";
$ Values. = "," $ route_char "";
} Else {
$ Fields. = ", route_char ";
$ Route_char = $ type;
$ Values. = "," $ route_char "";
}

$ Fields = substr ($ fields, 1, strlen ($ fields)-1 );
$ Values = substr ($ values, 1, strlen ($ values)-1 );

$ Result = mysqlquery ("insert into type ($ fields) values ($ values )");
...
Endif;/* end save */

// Upload by category ************************************ ************
If ($ func = "createtype "):

// Obtain your own id
$ Result = mysqlquery ("select * from type order
Id desc ");
$ Num = mysql_numrows ($ result );
If (! Empty ($ num )){
$ Cat = mysql_result ($ result, 0, "id ");
} Else {
$ Cat = 0;
}

// Determine the classification status
If ($ uid! = 0 ){
$ Result = mysql_query ("select * from type where id = $ uid ");
$ Type = mysql_result ($ result, 0, "type ");
$ Route_char = mysql_result ($ result, 0, "route_char ");
} Else {
$ Type = "parent category ";
}
Echo "<form action =" $ PHP_SELF? Func = save "METHOD = POST> ";

Echo "<table> ";
Echo "<tr> <td> category: $ type </td> </tr> ";
Echo "<tr> <td> Create category: <input type = text name =" type "SIZE = 10 MAXLENGTH = 100> </td> </tr> ";

Echo "<tr> <td> ";
$ Cat = $ cat + 1;
Echo "<input type = hidden name = id value =" $ cat "> ";
Echo "<input type = hidden name = uid value =" $ uid "> ";
Echo "<input type = hidden name = route_char value =" $ route_char "> ";
Echo "<input type = submit NAME =" Save "VALUE =" Save "> </td> </tr> ";

Echo "</table> ";
Echo "</form> ";
Endif;/* end createtype */

// Display the category ************************************ ************
If ($ func = "showtype "):

Echo "<table> ";

// Determine the classification status
If ($ uid! = 0 ){
$ Result = mysql_query ("select * from type where id = $ uid ");
$ Type = mysql_result ($ result, 0, "type ");
} Else {
$ Type = "parent category ";
}

Echo "<tr> <td> <a href =" $ php_self? Func = createtype & uid = $ uid "> Create a Category </a> </td> </tr> ";

Echo "<tr> <td> $ type </td> </tr> ";

$ Result = mysql_query ("select * from type where uid = $ uid ");
$ Num = mysql_numrows ($ result );

If (! Empty ($ num )){
For ($ I = 0; $ I <$ num; $ I ++ ){

$ Id = mysql_result ($ result, $ I, "id ");
$ Type = mysql_result ($ result, $ I, "type ");

Echo "<tr> <td> ";
Echo "<a href =" $ php_self? Func = showtype & uid = $ id "> $ type </a> ";
Echo "</td> </tr> ";
}
}

Echo "</table> ";
Endif;/* end showtype */
.....
.....

?>

 

 

The above program completes the basic creation, storage, and display of the unlimited classification, and then completes the various parts of the classification creation function.

Path Tracking
We have already introduced how to create a category. In the classification table, information about the two storage paths rout_id and rout_char is recorded. Without any processing, the program can only be sorted to the bottom-layer Classification and cannot be regressed (of course, the back key of the browser can be used for regressing, but this is incomplete for the program ), therefore, the information of rout_id and rout_char must be decomposed to complete the actual path indication.

The specific procedure is as follows:

Id: 4
Uid: 2
Type: Development Tool
Rout_id: 0: 1: 2: 4
Rout_char: System: linux: development tools

When the program goes to the classification 'development tools', in addition to displaying the path information, it also needs to be able to go to any category on the path. How can this problem be solved? Here we need to use the explode () function. Because rout_id and rout_char correspond to each other, we can break them down:

 

The Code is as follows: Copy code

 

$ Path = explode (":", $ rout_id );
$ Path_gb = explode (":", $ rout_char );

 

 

At this time, all the classification information is broken down. Now we need to restore the path information by link:

 

The Code is as follows: Copy code

 

For ($ I = 0; $ I ++ ){
$ A = $ I + 1;
Echo "<
Href = $ php_self? Func = showtype & uid = ", $ path [$ a],"> ", $ path_gb [$ I]," </a> :";
If (empty ($ path_gb [$ I]) {
Break;
}
}

 


The above Code adds a link to restore the path, because the implementation is infinite classification, there is no limit, so in for ($ I = 0 ;; $ I ++) has no range restrictions, but the condition for loop exit is that the value in $ path_gb [$ I] is null, insert this code into the program block of the category display layout:

 

 

The Code is as follows: Copy code

 

<?
.....
.....
// Display the category ************************************ ************
If ($ func = 'showtype '):

Echo "<table> ";

// Determine the classification status
If ($ uid! = 0 ){
$ Result = mysql_query ("select * from type where id = $ uid ");
$ Type = mysql_result ($ result, 0, "type ");

// ********* Newly added code ***************
$ Rout_id = mysql_result ($ result, 0, "rout_id ");
$ Rout_char = mysql_result ($ result, 0, "rout_char ");
$ Path = explode (":", $ rout_id );
$ Path_gb = explode (":", $ rout_char );
Echo "<tr> <td> ";
For ($ I = 0; $ I ++ ){
$ A = $ I + 1;
Echo "<
Href = $ php_self? Func = showtype & uid = ", $ path [$ a],"> ", $ path_gb [$ I]," </a> :";
If (empty ($ path_gb [$ I]) {
Break;
}
}
Echo "</td> </tr> ";
***********************

} Else {
$ Type = 'parent category ';
}

Echo "<tr> <td> <a href = '$ php_self? Func = createtype & uid = $ uid '> Create a Category </a> </td> </tr> ";

Echo "<tr> <td> $ type </td> </tr> ";

$ Result = mysql_query ("select * from type where uid = $ uid ");
$ Num = mysql_numrows ($ result );

If (! Empty ($ num )){
For ($ I = 0; $ I <$ num; $ I ++ ){

$ Id = mysql_result ($ result, $ I, "id ");
$ Type = mysql_result ($ result, $ I, "type ");

Echo "<tr> <td> ";
Echo "<a href = '$ php_self? Func = showtype & uid = $ id'> $ type </a> ";
Echo "</td> </tr> ";
}
}

Echo "</table> ";
Endif;/* end showtype */
.....
.....
?>

 

 

After this function block is completed, you can continue to display the classification information.

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.