Jump to content

Building a comments section


TechnoDiver
 Share

Recommended Posts

Hello Freakazoids, I hope the weekend saw you all happy & healthy and the new week promises the same for everyone.

This post isn't about any particular issue I'm having. Rather, I'm looking for some advice and direction from people with more experience than me.

As the title says, I'm in the process of building a comments section for a project. The stage I'm at now is figuring out the best way to implement it.

l'd like it to go deeper than just allowing replies to original comments - allowing replies to replies etc. Though it doesn't have to be infinitely so like Reddit where replies to replies to replies have replies ad infinitum.

My first thought was to do it as lists and store it in the comments column of the posts table in the DB. Where the array of original comments is named with the post_id, assign an id to the comment and name the response array with the comment_id, assign the response array an id, name another list (2nd level replies) with the 1st level response id etc etc.

Or using JSON in a similar manner building it deeper and deeper as replies accumulate.

Then I figured maybe it's best to make a separate comments table with a post_id column, and there could be a replies column too; but I wasn't clear in my head how I would use that with replies to replies etc.

I don't expect the project to ever be so big that it needs infinite replies like Reddit but I would like it to go 3 or 4 replies deep.

I've no doubt that I'm capable of doing this but I'm hoping for some advice from those who have done it before - your experience with it, the mistakes you've made along the way, things to consider that someone at my level would likely overlook, best practices and exactly what is the best way to implement this etc.

TIA to everyone who finds the time and will to respond, the best to you all

Link to comment
Share on other sites

Have a table for comments which contains a unique record for each comment/reply:

  • comment_id (id for each comment/reply)
  • post_id (the ID of the post that the comment/reply belongs to)
  • reply_id (The comment Id of the comment being replied to, or empty/0 if it was an initial comment)
  • user_id
  • datetime
  • comment
  •  . . . any other relevant info

 

  • Great Answer 1
Link to comment
Share on other sites

I have done comment sections in the past in PHP and what I found useful is have two database tables. One for the main reply (thread) and the other for the replies that way if the thread should be deleted or messed up somehow (though I never had that happened) then simply delete the main thread id to the replies.

Edited by Strider64
Link to comment
Share on other sites

On 10/25/2021 at 12:05 PM, Psycho said:

Have a table for comments which contains a unique record for each comment/reply:

  • comment_id (id for each comment/reply)
  • post_id (the ID of the post that the comment/reply belongs to)
  • reply_id (The comment Id of the comment being replied to, or empty/0 if it was an initial comment)

Hi, Psycho, I've finally gotten some smaller issues out of the way so I can start on this again. I was looking through you response and it pretty much makes sense to me except the third item (reply_id) I'm picturing comment_id as the primary key but I don't understand exactly what you're saying with the reply_id. How could it be empty or 0?

If you get the time and will do you mind going into a bit more detail on this?! Thanks, I'd love to read whatever else you'd have to say about it.

Link to comment
Share on other sites

On 10/25/2021 at 12:50 PM, Strider64 said:

I have done comment sections in the past in PHP and what I found useful is have two database tables. One for the main reply (thread) and the other for the replies that way if the thread should be deleted or messed up somehow (though I never had that happened) then simply delete the main thread id to the replies

Do you have a limited number of replies per initial comment or is it able to take an infinite amount (think like Reddit)??

Link to comment
Share on other sites

On 10/25/2021 at 10:05 AM, Psycho said:

Have a table for comments which contains a unique record for each comment/reply:

  • comment_id (id for each comment/reply)
  • post_id (the ID of the post that the comment/reply belongs to)
  • reply_id (The comment Id of the comment being replied to, or empty/0 if it was an initial comment)
  • user_id
  • datetime
  • comment
  •  . . . any other relevant info

 

 +1 from me for this.

 

Link to comment
Share on other sites

9 hours ago, TechnoDiver said:

Hi, Psycho, I've finally gotten some smaller issues out of the way so I can start on this again. I was looking through you response and it pretty much makes sense to me except the third item (reply_id) I'm picturing comment_id as the primary key but I don't understand exactly what you're saying with the reply_id. How could it be empty or 0?

If you get the time and will do you mind going into a bit more detail on this?! Thanks, I'd love to read whatever else you'd have to say about it.

Taking reddit as an example, anyone makes an original comment to a post it would not be replying to another person's comment. So, the reply ID of those comments would be empty/zero. However, if anyone replies to one of those initial comments, then you need to know which comment they replied to - in that case you put the comment_id that someone is replying to as the reply_id. Then, if someone replies to one of those comments you use it as the reply_id

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.