Table of Contents
回复讨论(解决方案)

PHP评论回复

Jun 23, 2016 pm 02:39 PM

本帖最后由 haoren026 于 2013-12-15 21:12:03 编辑

我知道这个问题非常非常初级。。我也搜索了很多帖子,但是都相当麻烦,或者我能力不够无法实现。

问题是这样的:
我在练习写博客系统,要实现评论回复功能,要求不高,访客评论,博主回复评论,不能对回复再回复,只有一层关系:评论-回复。就这么简单。

网上的方法的数据表结构大多如下:
id:评论id
pid:回复的评论id
content:评论内容

我想知道,当评论的记录与回复的记录之间隔了若干行,要怎样找到回复的记录,比如:

id pid
1  0
2  0
3  1  //回复与评论隔了若干行
4  0

这个问题在SQL排序重组里好难解决啊。。求助大神,万分感谢


回复讨论(解决方案)

pid=1 不就是id=1的评论的回复么?没有必要关心是不是挨着的吧

没太明白隔了若干行是干什么的...

pid=1 不就是id=1的评论的回复么?没有必要关心是不是挨着的吧
我想从数据库取出评论的时候是如下的:
id pid
1  0
3  1
2  0
4  0
这样我用php输出的时候就非常简单,按照id顺序输出,遇到pid不为0,视为博主回复,显示在被回复的评论下面。

没太明白隔了若干行是干什么的...
比如访客发了2条评论,博主回复了第1条评论,那此时数据库里的存储顺序为:
id pid
1  0
2  0
3  1  //回复与评论隔了1行

参看我3楼回复2楼的帖子

pid=1 不就是id=1的评论的回复么?没有必要关心是不是挨着的吧
我想知道有没有SQL语句能实现相关的评论和回复“上下挨着”,这样就不用php去重排了


没太明白隔了若干行是干什么的...
比如访客发了2条评论,博主回复了第1条评论,那此时数据库里的存储顺序为:
id pid
1  0
2  0
3  1  //回复与评论隔了1行

参看我3楼回复2楼的帖子

这个跟上下挨着没有关系 ,重要的还是把你的sql与逻辑处理好,不要依赖这种顺序来确保你的程序不出错,它不可能按你的想象的顺序来insert的



没太明白隔了若干行是干什么的...
比如访客发了2条评论,博主回复了第1条评论,那此时数据库里的存储顺序为:
id pid
1  0
2  0
3  1  //回复与评论隔了1行

参看我3楼回复2楼的帖子

这个跟上下挨着没有关系 ,重要的还是把你的sql与逻辑处理好,不要依赖这种顺序来确保你的程序不出错,它不可能按你的想象的顺序来insert的

我知道,用户发评论和博主回复的顺序是不规则的,所以数据库存储是无序的,我是想在从数据库取数据时,在SQL查询语句里添加排序功能等等,使之实现我想要的顺序,方便输出到网页上,而不是把无序的数据丢给PHP去解决。所以需要一定的数据库设计和SQL查询技巧




没太明白隔了若干行是干什么的...
比如访客发了2条评论,博主回复了第1条评论,那此时数据库里的存储顺序为:
id pid
1  0
2  0
3  1  //回复与评论隔了1行

参看我3楼回复2楼的帖子

这个跟上下挨着没有关系 ,重要的还是把你的sql与逻辑处理好,不要依赖这种顺序来确保你的程序不出错,它不可能按你的想象的顺序来insert的

我知道,用户发评论和博主回复的顺序是不规则的,所以数据库存储是无序的,我是想在从数据库取数据时,在SQL查询语句里添加排序功能等等,使之实现我想要的顺序,方便输出到网页上,而不是把无序的数据丢给PHP去解决。所以需要一定的数据库设计和SQL查询技巧
用order by不可以吗?找他们的共同处,先pid,再其他,一个一个排序

用order by不可以吗?找他们的共同处,先pid,再其他,一个一个排序
没有共同处,我想过了的

 不知道我正确理解了问题和要求没有。

我觉得对这个问题,有几种解决方法:

1. 一个是,既然最复杂的情况只能是“帖子-评论-评论的评论”,那么我就干脆设立一个新的表格,专门存放评论的评论。我觉得这么做比讨论什么order by要靠谱的多。

2. 一个是,帖子显示(应该没有问题),然后是评论显示(也没有问题),只是在foreach显示每条评论的时候,再调用一些query来显示评论的评论。

个人建议用第二种方式。

BTW,这个问题其实不初级。

固定层级为两层好说
http://sqlfiddle.com/#!2/e433e/1
这语句是跟mysql版主学的

select * from `comment` order by if(pid=0,id,pid) asc
Copy after login
 

这个语句要好好学习。

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)

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

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

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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

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.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

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 permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

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

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.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

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

See all articles