Home php教程 PHP开发 PHP advanced: achieve unlimited classification

PHP advanced: achieve unlimited classification

Dec 14, 2016 am 11:38 AM

  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 (www.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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles