Analysis
When we use PHP to make a website, classification is very important. Below the classification, we classify again. This second classification is called a sub-classification, but now most websites are only classified into the third category:
First classification (parent classification)-->Second classification (child classification)-->Third classification (grandson classification)
The more such related classifications, the more complicated and difficult it is to control the program and database Classification processing and control at the same level is very simple, because only one database is needed to record the classification at this level, such as: system, news and other classifications. Processing at this level is very simple, but For a website, one-level classification is not enough. It needs to be classified again, such as:
System-->linux, windows
News-->linux news, windows news
This way the classification will be clearer, 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 clearer, we continue to classify:
linux-->System tools, kernel, programming languages, development tools
...
When the classification reaches the third level, the processing of information becomes clearer. That is to say, in order to process the information clearly, the more detailed the classification, the more convenient it is. This makes it easier to process the information and make it easier for netizens to find it with a clear purpose. Required information, but as the classification continues to be refined, it will become more and more difficult to control the program and database.
Difficulty 1: How to deal with these related kinship classifications in the database?
Difficulty 2: How Use PHP to complete this clear relationship?
This kind of multi-level and detailed classification is a problem that every PHP programmer must solve, because the classification problem of making a good and excellent website is inevitable, and solving this problem It is quite complicated, and the biggest problem is the classification processing of the database, because if the database is not handled properly, it will bring a huge workload and even have to re-plan the database...
This is not an exaggeration, because many people are... For database processing, we will use the first-level classification method to build a database. I also adopted this method to handle classification at that time. Since most websites are classified into the third level, there are only three classification databases in the database for processing. But when it is necessary to continue to classify downwards, the disadvantages of this approach are revealed, because the further down the classification, the workload and program volume will increase dramatically.
The method I want to introduce is how to use a classification database Establish a classification method for infinite downward grading. Readers who have used Windows all know that you can create infinitely tiered directories using Windows folders. You can continue to create directories under the directory, so that they can be divided endlessly. Linux directory creation also has this kind of method. Function, the method I introduced is the same as this form.
2. Database planning
--------------------------------- ----------------------------------
I talked about the complexity of classification earlier, so how to plan the database becomes very important to achieve unlimited classification. An important step.
I once introduced the database planning of the forum. Yes, the forum can achieve unlimited follow-up. Unlimited classification is an extension of this form. Classification also has this kind of child-father relationship, so the classified database is how There are several difficulties in establishing this child-father relationship.
1) How to handle the information storage of each category;
2) How to handle the kinship relationship of the categories;
3) How to handle the query for information;
Affinity relationship The database processing is similar to the database processing of the forum. Here, a type database is built to process categories:
Create fields:
id(int): used to record the natural serial number of each category
uid(int): used to record the The id number of the parent category of the category
type(char): the name of the category
roue_id(varchar): the affinity tree, connecting with the id of: 0:2:10:20: to indicate the parentage relationship
roue_char(varchar): the affinity tree , similar to: system: linux: development tools: gcc: (It doesn’t matter whether this field exists or not. In order to understand the relationship more conveniently, of course character expression is more direct than numerical expression^o^, but it is best to add this field)
Such an unlimited category table is established. Next, you need to establish a database to store information. It is most convenient to process and query a table, so here is a table to store information type_message:
id (int): the serial number of the message;
typeid (int): ID number of the category;
title(varchar): message title;
message(text): message content;
time: time when the message was created;
These two data tables can complete unlimited classification Task (the auxiliary fields of the two tables have not been added, readers can add them by themselves).
All remaining tasks will be handled by php.
3. Program control
-------- -------------------------------------------------- --
This is the most complicated and arduous step in the function of realizing unlimited classifications. First, let’s take a look at the steps that need to be completed in the program:
1) Create category uploads;
2) Create information uploads;
3) Clearly display each category and The relationship between them;
4) Processing query function;
5) How to handle the editing and deleting functions;
The most difficult of these five steps is the fifth step, because editing and deleting categories involves one-dimensional issues.
I will describe the PHP program one by one below. Control:
1) Create category upload
Before introducing this function, let’s introduce the explode() function. This is a string processing function, used to decompose strings. Specific usage, example:
explode" The numbers in 0:1:2:3:4"
$val='0:1:2:3:4';
$rid=explode(":",$val);
after explode( ) Function processing, all the numbers in $val are decomposed into the $rid array. When you want to quote, just print: echo '$rid[0],$rid[1],$rid[2]..."; That's it. The.explode() function plays a very important role in the entire classification process. Now let’s introduce the program control of non-current classification.
We can assume that there is a total classification of 0, and all classifications are its descendants. Now let’s Establish the first classification 'system' and take a look at its storage form in the database:
id | uid | type | rout_id | rout_char
1 | 0 | system | 0:1 | system
Then it is divided below' Linux':
id | uid | type | rout_id | rout_char
2 | 1 | Linux| 0:1:2 | System: Linux
The above is the form of database storage. Now let’s complete the php code, which is related to the forum The code is very similar. All we have to do is put the id of the category into uid, and the uid of the parent category is 0. Let’s take a look at the code:
.....
....
//Set the default 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=="") {
/ /Get 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 */
//Category upload********************** *******************************
if ($func=='createtype'):
//Get your own id
$result = mysqlquery("select * from type order by
id desc");
$num=mysql_numrows($result);
if (!empty($num)) {
$cat = mysql_result($result, 0,"id");
} else {
$cat=0;
}
//Determine the status of the classification
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 "
创建分类 |
$type |
"; echo "$type"; 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 " |
创建分类 |
$type |
"; echo "$type"; echo " |