PHP: Create an Infinitus comment module
The comment module of my graduation project was originally completed using the Duoshuo plug-in, but now I hope to be able to manage the comment content myself, so I started writing the comment module myself. The specific preparation is to adopt a similar structure to the next comment, that is, the first-level comments are displayed directly below the article, while the second- and third-level comments are displayed below the first-level comments, as shown in the following figure:
Comment structure
I think this can be said to be an application of Infinitus classification. To be precise, it is the application of descendant trees. After classifying descendant trees, loop Output the content and form a comment (Friends who don’t understand Infinitus classification can read my article Principles and Implementation of Infinitus Classification).
Of course, there are other key points to truly complete the function of Infinitus replying to comments. Let’s talk about how I completed Infinitus comments.
Database Design
First of all, it is the design of the data table. If it is a forum system, the comment data can be divided into two tables. One table stores the comment information, including the user id of the post or the user id of the reply, and the post id## of the reply. #, Reply time, etc.; another table stores the content of the comment, including the topic of the post and the content of the reply.
What I have completed is the comment module of the article. It is not divided into two tables. I directly put the content and information of the comment together, as follows: Column nameColumn typeColumnDescriptioncomm_idINTUNSIGNED PRIMARY Primary Keyuser_idINTUNSIGNED NOT useridparent_idINTUNSIGNED NOT NULL DEFAULT 0Parent of commentartcile_idINTUNSIGNED NOT NULL DEFAULT 0 Commented article idcomm_contTEXTCommented contentcomm_INTUNSIGNED NOT NULL DEFAULT 0SQL statement:
1 2 3 4 5 6 7 8 |
|
This structure is the basis for completing Infinitus reply. It can also be clearly seen that the retrieved data can be well classified by Infinitus.
Structure analysis of comments
It is very easy to complete a comment module. Put the comments into the database, then retrieve them and place them in html. The comment module can also be completed. However, this structure is very messy and disorderly. If you want to complete a comment module like a comment, you have to use a special method.
Then, we need to take a closer look at the structure of comments.
Comment structure
Combined with the above data table structure, we can infer that the data taken out from the data table and classified by Infinitus, Its structure should be like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Why do you say that? You can clearly see that the second- and third-level comments are wrapped in the first-level comments, and the second- and third-level comments are parallel relationships; therefore, the second- and third-level comments are descendant nodes of the first-level comments, and Second-level and third-level comments are parallel nodes, and there is no parent-child relationship.
Therefore, it can be concluded that the classified data has and has only one descendant node. Whether a multi-level comment is a reply to a first-level comment or not, as long as it is within the scope of a first-level comment, then its parent node It must be a first-level review.
So, how is the @XXXX
in the second-level and third-level replies implemented? Actually, I was troubled here for a long time. I expected to use a self-join to the table to complete, but this does not work, destroying the structure described above. Finally, I got the answer from the requested json data, please see the commented json data:
It’s a bit blurry after uploading. Students can use the plug-in on Firefox or Google Chrome to observe the JSON data.
Focus on thecompiled_content field, and you can infer that it stores
@XXXX directly into the database. In this way, the problem is solved. At the same time, observing the json data can also verify that the structure I mentioned above is correct.
1 2 3 4 |
|
function is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
PS
This is just a form of comment. If there is a staircase-like structure, this implementation is simpler, as shown below:1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 |
|
然后直接循环输出,并将lev作为属性打印在html中,最后利用js读取lev,并根据不同的等级分配不同的margin-left即可,它会随着margin的不同而排列在不同的位置,如下:
1 2 3 4 5 6 7 8 9 |
|
Attribute | |||
---|---|---|---|
KEY AUTO_INCREMENT | |||
NULL DEFAULT 0 | |||
time | Comment Post time |
The above is the detailed content of PHP: Create an Infinitus comment module. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

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,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

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.
