The best way to implement 2 nested comments system. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The best way to implement 2 nested comments system.

like a Facebook, Twitter, or even sololearn did. Could you told me how? in php? just for basic stuff. thanks

9th May 2018, 10:30 AM
Muhamad Anggayani
Muhamad Anggayani - avatar
3 Answers
+ 1
If you want to make one from scratch, you could use the following MySQL script and render the comment tree with nested div elements. I would give each div a small margin-left or padding-left so that the more nested ones are indented more. You could model the comments and users that write them like this: create table users ( id int primary key auto_increment ); create table comments ( id int primary key auto_increment, parent_comment_id int references comments(id), -- nullable parent comment for nesting commenter_user_id int not null references users(id), -- who commented message varchar(255) not null, -- what the comment says created_at timestamp -- when the comment was made ); That script assumes you have just 1 nested comment section. If you have multiple blogs of comments, you could add a blog table and include a reference to it in your comments table. A top-level/unnested comment is the only kind that would critically need to reference the blog but specifying a copy of it in nested comments should simplify querying and joining comments on a specific blog.
28th Aug 2020, 10:04 PM
Josh Greig
Josh Greig - avatar
+ 1
Josh Greig Is it possible for 'parent_comment_id' column to introduce a circular reference issue?
28th Aug 2020, 10:52 PM
Ipang
+ 1
Ipang, yes, the schema I suggested won't complain if a cycle is introduced. Something like that could be prevented by not implementing any feature that updates the parent_comment_id of an existing comment. The only way a cycle could be introduced is if an existing comment got its parent_comment_id changed to point at one of its descendants. Anyone with access to the write changes to the SQL database could represent a cycle but a malicious user with that access could do a lot more damage anyway.
28th Aug 2020, 11:39 PM
Josh Greig
Josh Greig - avatar