Home > php教程 > PHP开发 > body text

PHP advanced: achieve unlimited classification

黄舟
Release: 2016-12-14 11:38:49
Original
1317 people have browsed it
  1. 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 "

    ";

    echo "";
    echo "";
    echo "< ;/tr>";

    echo ""; 

    echo "
    Category:$ type
    Create category:
    ";
    $cat=$cat+1;
    echo "" ;
    echo "";
    echo ""; 
    echo "
    "; 
    echo "
    "; 
    endif; /* end createtype */ 

    //显示分类************************************************ 
    if ($func=='showtype'): 

    echo ""; 

    //判断分类的状态 
    if ($uid!=0) { 
    $result=mysql_query("select * from type where id=$uid"); 
    $type=mysql_result($result,0,"type"); 
    } else { 
    $type='父分类'; 


    echo ""; 

    echo ""; 

    $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 "$type"; 
    echo "
    "; 
    endif; /* end showtype */ 
    ..... 
    ..... 

    ?> 

    以上的程序便完成了无限分类的基本创建,存储和显示,接着就是完善分类创建功能的各个部分了.

    4.路径跟踪 
    ------------------------------------------------------------ 
    前面已经介绍过了分类的创建实现方法,在分类表里记载了 rout_id 和 rout_char 这两个存储分类路径的信息,在不做任何处理的情况下,程序只能够顺序下到最底层的分类而无法倒退(当然可利用浏览器的 back 键倒退,但这对程序来说是不完整的),因此必须将 rout_id 和 rout_char 的信息分解出来完成实在的路径指示. 

    具体的做法,假如数据库记载了这么一条分类信息: 

    id:4 
    uid:2 
    type:开发工具 
    rout_id:0:1:2:4 
    rout_char:系统:linux:开发工具 

    当程序走到分类'开发工具'上时,除了要求显示路径信息外还要求能够去到路径上的任一分类中,该怎么做能?这里就需要用到 explode() 函数了.因为 rout_id 和 rout_char 是对应关系的,所以可将它们分解: 

    $path=explode(":",$rout_id); 
    $path_gb=explode(":",$rout_char); 

    这时所有分类信息都被分解了,现在要做的就是以链接的方式还原路径信息: 

    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; 



    上面这段代码就实现了加链接还原路径的功能,因为实现的是无限分类,因此是没有上限的,所以在 for($i=0;;$i++) 里没有范围限制,而设置循环退出的条件是 $path_gb[$i] 中的值为空,将这段代码插入类别显示版面的程序块内就行了: 

    ..... 
    ..... 
    //显示分类************************************************ 
    if ($func=='showtype'): 

    echo ""; 

    //判断分类的状态 
    if ($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 "";
    //******** end *********************** 

    } else { 
    $type='父分类'; 


    echo ""; 

    echo ""; 

    $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 "
    "; 
    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 "
    "; 
    endif; /* end showtype*/
    .....
    .....
    ?>

    After completing this function block, you can Continue to display classified information... For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template