When we use php to create a website, classification is very important. The second classification is called subclassification under classification. Currently, most websites are classified only into the third category.
1. Analysis
When we use php to create a website, classification is very important. The second classification is called subclassification under the classification. Currently, most websites are classified only into the third category:
First category (parent category) --> second category (sub-category) --> third category (Sun category)
The more kinship classification, the more complicated and difficult the control of 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 message categories, the 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, windows
Messages> linux messages and windows messages
In this way, we can understand the classification. at least we can understand that the system includes linux and windows, and messages include linux messages and windows messages. in order to make the information materials clearer, we will continue to classify them:
Linux --> System Tools, kernels, programming languages, and development tools
...
When classification reaches Level 3, information materials are processed more clearly. that is to say, the more specific the classification is, the more convenient it is, this makes it easy to process information and help users find the required materials clearly. However, with the continuous refinement of the classification, it will become increasingly difficult to hold the program and database.
Hard 1: how to deal with these interrelated kinship classifications in the database?
Hard 2: how to use php to achieve this clear relationship?
This multi-classification and fine-grained classification is the title that every php programmer must address. it is inevitable to create a good and outstanding website classification title, the solution to this title is quite complicated. the biggest title is database classification processing. 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 use the first-level classification to create a database for database processing, I also used this method to process classification, because most websites are classified to the third-level, therefore, the database only needs three classification databases for processing. however, when the downward classification is required, the disadvantages of this approach are revealed. the more you score, the more workload, the larger the program volume ..
This method is to use a classification database to create an unlimited downward classification method. readers who have used windows know windows folders to create an infinitely hierarchical directory, you can establish a directory under the Directory, so that the endless division, the creation of the Linux directory also has this effect, I first introduced this method and the situation similar.
2. database plan
------------------------------------------------------------
As we talked about the complexity of classification, it is very important to plan a database to implement unlimited classification.
I have previously introduced the database plan of the Forum. good news is that the forum can achieve unlimited follow-up. the infinite classification is the expansion of this situation, and the classification is also the Association of this parent, therefore, the classification database is how to establish and understand the sub-parent relationship, which has 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): a type of name.
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 table is created. Next, you need to create a database for storing information. it is most convenient to 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 type;
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 Help fields of the two tables are not added, and you can participate in the task on your own ).
All the remaining tasks are handled by php.
3. program control
This step is the most complex and difficult to implement the infinite classification function. First, let's look at the steps that need to be completed by the program:
1) create category Upload;
2) create information Upload;
3) clearly show the relationships between different categories;
4) query processing efficiency;
5) How to deal with the functions of compilation and deletion;
The fifth step is the most difficult of the five steps, because the compilation and deletion of categories involves a one-to-one title.
Next I will describe php program control one by one:
1) create category Upload
Before proceeding with this function, let's first take a look at the explode () function. this 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 the $ val are decomposed into the $ rid array. to reference the data, Print: echo '$ rid [0]. $ rid [1], $ rid [2]... '; that's all. the explode () function plays a very important role in all classification processing.
We can assume that a total Category is 0, and all categories are its descendant categories. now we will establish the first classification 'system' to see its storage situation 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 situation of database storage. now we can complete the php code, which is similar to the code of the Forum. 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:
.....
.....
// 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 '';
Endif;/* end createtype */
// Display the category ************************************ ************
If ($ func = 'showtype '):
Echo'
';// Determine the classification statusIf ($ uid! = 0 ){$ Result = mysql_query ('select * from type where id = $ uid ');$ Type = mysql_result ($ result, 0, 'type ');} Else {$ Type = 'parent category ';}Echo'
| Create Category |
';Echo'
| $ Type |
';$ 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'
'; Echo '$ type '; Echo' |
';}}Echo'
';
Endif;/* end showtype */
.....
.....
?>
The above procedures have completed the creation, storage, and display of the infinite classification, and then we have improved the functions of classification creation.
4. path tracking
Previously, we have introduced how to create a category. in the classification table, we recorded the information of the two storage paths rout_id and rout_char. without any processing, the program can only be sorted to the lowest-level 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.
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 requesting to display the path information, it also requests 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:
$ 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 using the link method:
For ($ I = 0; $ I ){
$ A = $ I 1;
Echo 'href = $ php_self? Func = showtype & uid = ', $ path [$ a],'> ', $ path_gb [$ I],': ';
If (empty ($ path_gb [$ I]) {
Break;
}
}
The above code implements the function of adding a link to restore the path. because there is no limit to the implementation of unlimited classification, in for ($ I = 0; $ I) there is no category restriction, and the condition for setting loop exit is that the value in $ path_gb [$ I] is null. just insert this code into the block where the layout is not displayed:
.....
.....
// Display the category ************************************ ************
If ($ func = 'showtype '):
Echo'
';// Determine the classification statusIf ($ uid! = 0 ){$ Result = mysql_query ('select * from type where id = $ uid ');$ Type = mysql_result ($ result, 0, 'type ');***************$ 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'
'; For ($ I = 0; $ I ){ $ A = $ I 1; Echo 'href = $ php_self? Func = showtype & uid = ', $ path [$ a],'> ', $ path_gb [$ I],': '; If (empty ($ path_gb [$ I]) { Break; } } Echo' |
';***********************} Else {$ Type = 'parent category ';}Echo'
| Create Category |
';Echo'
| $ Type |
';$ 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'
'; Echo '$ type '; Echo' |
';}}Echo'
';
Endif;/* end showtype */
.....
.....
?>
After this function block is completed, the display of sustainable classification information is realized...