简体   繁体   中英

How to display reply to comments together with the parent comments?

Hi I'm wondering what's the best apppraoch to display reply to comments together with the parent comments.


1 way I learned is to store the replies and parent comments in the same table, while giving the replies an index "parent comment id". The "parent comment id" would be null for the parent comments. When displaying the comments, simply display all together and use an if (parent comment id=null) statement to append the replies to parent comments.

Pseudo-code to demonstrate:

Mysql comment table: comment_id|parent_comment_id|comment_body|...

Php:

foreach

If (parent_comment_id=null) { Display comment_body }

If (parent_comment_id<>null) { Display comment_body }

end of foreach

The other way I'm thinking is to store the replies in a separate table, and pull the replies only when they have a parent comment displayed.

Pseudo-code to demonstrate: Mysql comment table: comment_id|comment_body

Mysql reply table: reply_id|parent_comment_id|reply body

Php: foreach

Display comment_body

If (parent_comment_id=comment_id) { Display reply_body }

end of foreach


What is the better way to do it?

Thanks a lot,

You can try NoSQL DB for this . ex MongoDB. it's better then MySQL.

Use Json Display Result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM