PHP comment Reply

Source: Internet
Author: User
PHP comments reply to this post, which was last edited by haoren026 at 21:12:03 on

I know this question is very, very elementary .. I also searched a lot of posts, but it was quite troublesome, or I was not able to achieve it.

The problem is as follows:
I am practicing writing a blog system. to implement the comment reply function, the requirements are not high. The visitor comment and the blogger replies to the comment. there is only one layer of relationship: Comment-reply. That's simple.

The data table structure of the online method is mostly as follows:
Id: comment id
Pid: the id of the reply comment.
Content: comment content

I want to know how to find the reply record when the record of the comment is separated from the record of the reply, for example:

Id pid
1 0
2 0
3 1 // The reply is separated from the comment by several lines
4 0

This problem is hard to solve in SQL sorting and reorganization .. Thank you!


Reply to discussion (solution)

Pid = 1 is not the reply of the comment with id = 1? You don't have to worry about it.

It's not clear what a few lines are doing...

Pid = 1 is not the reply of the comment with id = 1? You don't have to worry about it.
The comment I want to retrieve from the database is as follows:
Id pid
1 0
3 1
2 0
4 0
In this way, the php output is very simple and output in the order of IDS. when the pid is not 0, it is regarded as the reply of the blogger and displayed below the reply comment.

It's not clear what a few lines are doing...
For example, if a visitor sends two comments and the blogger replies with 1st comments, the storage sequence in the database is as follows:
Id pid
1 0
2 0
3 1 // The reply is separated from the comment by one line

Refer to my post on the third floor to reply to the second floor.

Pid = 1 is not the reply of the comment with id = 1? You don't have to worry about it.
I want to know if there are any SQL statements that can enable the related comments and replies to be "squatting" so that php does not need to be rearranged.


It's not clear what a few lines are doing...
For example, if a visitor sends two comments and the blogger replies with 1st comments, the storage sequence in the database is as follows:
Id pid
1 0
2 0
3 1 // The reply is separated from the comment by one line

Refer to my post on the third floor to reply to the second floor.

This has nothing to do with the context. it is important to handle your SQL and logic well. do not rely on this order to ensure that your program will not go wrong, it cannot be insert in the order you want.



It's not clear what a few lines are doing...
For example, if a visitor sends two comments and the blogger replies with 1st comments, the storage sequence in the database is as follows:
Id pid
1 0
2 0
3 1 // The reply is separated from the comment by one line

Refer to my post on the third floor to reply to the second floor.

This has nothing to do with the context. it is important to handle your SQL and logic well. do not rely on this order to ensure that your program will not go wrong, it cannot be insert in the order you want.

I know that the order in which users send comments and replies is irregular, so the database storage is unordered. when I want to retrieve data from the database, add the sorting function in the SQL query statement so that the order I want can be easily output to the web page, rather than throwing unordered data to PHP. Therefore, some database design and SQL query skills are required.




It's not clear what a few lines are doing...
For example, if a visitor sends two comments and the blogger replies with 1st comments, the storage sequence in the database is as follows:
Id pid
1 0
2 0
3 1 // The reply is separated from the comment by one line

Refer to my post on the third floor to reply to the second floor.

This has nothing to do with the context. it is important to handle your SQL and logic well. do not rely on this order to ensure that your program will not go wrong, it cannot be insert in the order you want.

I know that the order in which users send comments and replies is irregular, so the database storage is unordered. when I want to retrieve data from the database, add the sorting function in the SQL query statement so that the order I want can be easily output to the web page, rather than throwing unordered data to PHP. Therefore, some database design and SQL query skills are required.
Can order by be used? Look for their common location, first pid, then others, one by one

Can order by be used? Look for their common location, first pid, then others, one by one
Nothing in common. I thought about it.

I don't know if I understand the problems and requirements correctly.

I think there are several solutions to this problem:

1. one is that, since the most complex case is only "post-Comment-comment", I will simply set up a new form to store comment. I think this is more reliable than discussing order.

2. one is that the post is displayed (there should be no problem), and then the comment is displayed (there is no problem), but when foreach displays each comment, call some queries to display comments.

The second method is recommended.

BTW, this problem is actually not preliminary.

The fixed level is two layers.
Http://sqlfiddle.com /#! 2/e433e/1
This statement is used by the mysql moderator.

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

This statement should be well learned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.