Implement unlimited classification

xiaoxiao2021-03-06  40

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 "

";

Echo "

"; Echo "
Category: $ TYY "; echo "
Create Category: "; echo"
"; $ CAT = $ Cat 1; echo" "; echo" "; echo" "; echo" ";

Echo ""; echo ""; Endif; / * end createtype * /

// Show classification **************************************************** ** IF ($ func == 'showType'):

Echo "

";

// Judgment the status of the classification IF ($ UID! = 0) {$ result = mysql_query ("Select * from type where id = $ uid"); $ type = mysql_result ($ results, 0, "type");} else {$ TYPE = 'parent classification';}

Echo "

$ type "; ECHO " ";}} Echo ""; Endif; / * end showtype * / ..... .....

?> 4. Path tracking

-------------------------------------------------- ------------

The creative implementation method of classification has been described above, which records the information of the two storage classification paths of Rout_ID and ROUT_CHAR in the classification table. When do not do any processing, only the program can only go to the bottom of the bottom layer. Return (of course, you can use the browser's back button, but this is incomplete to the program), so it must decompose the information of Rout_ID and ROUT_CHAR to complete the real path indication.

Specific practice, if the database records this classification information:

ID: 4 Uid: 2 TYPE: Development Tools Rout_id: 0: 1: 2: 4 Rout_char: System: Linux: Development Tool

When the program comes to the classification 'development tool', in addition to requesting the display path information, it is required to go to any class of the path on the path. Here is to use the exPlode () function. Because Rout_ID and Rout_char is the corresponding relationship, so you can decompose them:

$ PATH = EXPLODE (":", $ rout_id); $ PATH_GB = EXPLODE (":", $ rout_char);

At this time, all classification information is decomposed. Now it is to restore the path information in a link:

For ($ i = 0 ;; $ i ) {$ A = $ i 1; echo ":"; if (EMPTY ($ PATH_GB [$ I])) {Break;}}

The above code implements the function of the link restore path, because the implementation is unlimited classification, so there is no upper limit, so there is no scope limit in FOR ($ I = 0; $ i ), and set loop exits The condition is the value of $ PATH_GB [$ I] empty, and this code is inserted into the category display of the layout of the program.

Echo "

";

// Judgment the 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 = evtern (":", $ rout_id); $ PATH_GB = EXPLODE (":", $ rout_char); ECHO "

"; for ($ I = 0 ;; $ i ) {$ A = $ i 1; echo "", $ PATH_GB [$ I], "< / a>: "; if (Empty ($ PATH_GB [$ I]))) {Break;}} Echo" "; // ******* * **** *******************} else {$ TYPE = 'parent classification';}

Echo "

$ type "; ECHO " ";}}

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

After completing this functional block, you can continue the display of the classified information ...

转载请注明原文地址:https://www.9cbs.com/read-62463.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.041, SQL: 9