search
HomeBackend DevelopmentPHP Tutorialjavascript - 请问评论系统的层级数据结构要怎么取最合适?

我想自己写一个wordpress的评论系统,当然是用wordpress的数据表结构了。
但是在有个地方有点疑惑,就是如何去弄清这个层级。

数据库结构如下

javascript - 请问评论系统的层级数据结构要怎么取最合适?

其中id是自动生成的,postid表示评论在哪篇文章,approved表示是否过审(0表示没有),parent表示评论的父级Id(回复评论),content是内容。

正常的输出效果应该是
javascript - 请问评论系统的层级数据结构要怎么取最合适?

在这里,我应该如何写php,才可以达到这种效果...我就是脑里没有这种数据结构调用的概念。它的过程

首先肯定是调用这个postid为99的文章,这样数据库的内容就同我的第一个截图一样。
接下来我的想法是想输出层级为0的评论,但是其他的层级什么时候插入呢?

回复内容:

我想自己写一个wordpress的评论系统,当然是用wordpress的数据表结构了。
但是在有个地方有点疑惑,就是如何去弄清这个层级。

数据库结构如下

javascript - 请问评论系统的层级数据结构要怎么取最合适?

其中id是自动生成的,postid表示评论在哪篇文章,approved表示是否过审(0表示没有),parent表示评论的父级Id(回复评论),content是内容。

正常的输出效果应该是
javascript - 请问评论系统的层级数据结构要怎么取最合适?

在这里,我应该如何写php,才可以达到这种效果...我就是脑里没有这种数据结构调用的概念。它的过程

首先肯定是调用这个postid为99的文章,这样数据库的内容就同我的第一个截图一样。
接下来我的想法是想输出层级为0的评论,但是其他的层级什么时候插入呢?

这不是典型的树结构吗?只是将其压平了存到数组里。
postid是为了让你一个查询就能搞定,不至于分别查一下每个节点
至于怎么插入,如果你打算一次插入的话,这就跟你渲染模板的代码有关了啊,重点还在你的html和css上。

也可以分成两个查询:首先正常渲染页面只有一级评论,然后来一个ajax查询把补充的填进去(百度贴吧)。

但因为涉及分页的问题就比较麻烦了。

像是知乎那样就尽量降低了复杂度,当然知乎是没楼中楼的,只有一个回复XXX,这样的实现就简单许多,而且也不用担心分页了。

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.