Analysis
When we use a PHP to make a website, the classification is very important. The second classification is called a second class below, and now most websites are divided into third categories:
First classification (parent classification) -> second classification (sub-classification) -> third classification (grandsche)
This kind of pro-class class, the more complex difficulties in the control of the procedures and databases. The classification and control of the same level is very simple, because only one database is required to record this level of classification, such as: System, news, etc. are very simple at this level, but it is not enough for a website, and it needs to be classified, such as:
System -> Linux, Windows News -> Linux News, Windows News
This classification is clear, at least let people understand that the system includes Linux and Windows, and the news includes Linux news and Windows news, in order to make the information more clear, then continue classification:
Linux -> System Tools, Core, Programming Language, Development Tools ...
Classified to the third level, the processing of information is clearer, that is, in order to handle the information, the more convenient, so that it is convenient to handle information, it is convenient to find the information, but With the continuous refinement classification, it will be more and more difficult to control in procedures and databases.
Difficult: How to deal with these interposedary classifications in the database? Difficulties: How to complete this clear relationship with PHP?
This classification is a problem that each PHP programmer must solve, because making a good and outstanding website classification problem is inevitable, and solving this problem is quite complicated, with the biggest problem is The database classification processing, because if the database handling improperly brings huge workload or even re-planning the database ...
This is not an exaggeration, because many people will use the first-class classification to establish a database in the database processing, I was also taking this method to handle classification, because most websites were divided into the third level, so the database only Three classified databases are required to process. But when you need to continue to classify down, the drawbacks of this practice are revealed, because the more down, the workload, the amount of data will increase ..
This method I want to introduce how to build an unlimited down-level classification method with a classified database, using Windows readers know the Windows folder to establish an unlimited grading directory, and continue to establish a directory below, this is not finished Differential points, Linux's directory creation also has this function, and this method I introduce is the same as this form.
2. Database planning ------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------
I have introduced the database planning of the forum. Nice Forum can achieve unlimited follow-up, unlimited classification is this form of expansion, classification is also the association relationship of this subfather, so the classified database is how to establish this kind of parent relationship There are several difficulties in this.
1) How to deal with information storage of each classification; 2) How to handle the relationship between classification; 3) How to handle queries for information;
The database processing of the property relationship is similar to the database processing of the forum. It is used to build a TYPE database to process classification:
Creating a field: ID (int): Natural serial number UID (Int) used to record each classification: Type (char) used to record the parent classification of the classification: Category RouE_ID (varchar): Private tree, in: 0: 2: 10: 20: ID connection Indicates the pro-relationship ROUE_CHAR (VARCHAR): Private tree, Similar: System: Linux: Development Tool: GCC: (This field does not matter, in order to more easily understand the relationships Of course, characters are more direct than digital representation, but it is best to add this field)
Such an unlimited classification class is built, followed by establishing a database of stored information, handling a table for a table, so establish a table storage information type_message:
ID (int): information of information; typeid: Id number of category; title (varchar): information title; Message (text): Information content; Time: Information is established;
These two data sheets can complete the task of unlimited classification (the two table auxiliary fields are not added, readers can join them themselves).
The remaining tasks are all handed over to PHP processing. 3. Program control
-------------------------------------------------- ---------- Implementing infinite classification This step is the most complex hard work, first look at the steps that the program needs to be completed:
1) Creating a classification upload; 2) Creating information Upload; 3) Explicitly display the relationship between each classification and its; 4) Processing query function; 5) How to handle editing and deletion;
The most difficult thing in these five steps is the fifth step because the editing and deletion of the classification involves a problem.
Let me describe the program control of PHP one by one.
1) Create a classification upload
Before introducing this feature, let's introduce the explode () function, which is a string handler, used to break down the string, specific usage, example:
Decompose the number of "0: 1: 2: 3: 4"
$ VAL = '0: 1: 2: 3: 4'; $ RID = EXPLODE (":", $ VAL);
After the explode () function, all the numbers in the $ VAL are broken down into the RID array. Just print when referenced: Echo '$ RID [0], $ RID [1], $ RID [2] .. "Immediately, it plays a very important role in the entire category processing, so it is now introducing programs control that are unable to classify.
You can assume a total classification 0. All classifications are its descendants classification, and now create the first classification 'system' to see if it is stored in the database:
ID | UID | TYPE | ROUT_ID | ROUT_CHAR 1 | 0 | System | 0: 1 | System
Then follow the next 'Linux':
ID | UID | TYPE | ROUT_ID | ROUT_CHAR 2 | 1 | Linux | 0: 1: 2 | System: Linux
The above is the form of the database store, now to complete the PHP code, this is very similar to the code of the forum, what we have to do is put the classification ID into the UID, and the parent classification UID is 0, let's take a look at the code. :
..... .....
// Set the default page IF (Empty ($ FUNC)) $ func == 'showtype';
// Set the UID 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 == "") {
// Get the route_idiff ($ 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"; // forms its 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);
$ results = mysqlquery ("INSERT INTO TYPE ($ VALUES)"); ... ENDIF; / * End Save * /
// Classification upload ***************************************************** ** IF ($ func == 'CreateType'):
// Take your own ID $ result = mysqlQuery ("Select * from type order by id desc"); $ Num = mysql_numrows ($ result); if (! Empty ($ cat = mysql_result ($ Result, 0, "ID");} else {$ CAT = 0;}
/ / Judgment the status IF ($ uid! = 0) {$ result = mysql_query ("SELECT * from type where id = $ uid"); $ TYPE = mysql_result ($ Result, 0, "Type"); $ route_char = mysql_result ($ results, 0, "route_char");} else {$ type = 'parent classification';} echo "