Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make replies to comments? (PHP)

Tags:

php

mysql

I want to create something like reddit where they have comments, then replies to the comment, then reply to the reply.

What type of database structure do they use so:

1. they keep track of all the comments to a posting
2. a reply to a comment
3. a reply to a reply

All I have right are is just a posting and a bunch of comments relating to it like..

POSTING TABLE
posting_id | title | author

COMMENTS TABLE
comment_id | posting_id | comment

REPLIES TABLE
????

How do I relate the comments to the replies? What type of css do they use to give replies that indented space?

EDIT: Thanks for the answers! Now my only question how do I indent the replies? Such as..

you like food
     yes I love italian
        Yes i do like it too
     chinese is best
like image 645
jpjp Avatar asked May 28 '10 23:05

jpjp


People also ask

How do you make comments statements in PHP?

Single-line PHP Comments To leave a single-line comment, type two forward slashes (//) followed by your comment text. All text to the right of the // will be ignored. You can also use a hash symbol (#) instead of // to make a single-line comment.

How do you comment multiple lines in PHP?

In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /* */.


2 Answers

You can add another column to your comments table specifying parent_comment_id where you populate it with the ID of the comment (or reply) the user is replying to. In the case where the comment is a direct reply to the post (not a reply to a comment) this column would be null.

like image 194
Matt S Avatar answered Sep 22 '22 10:09

Matt S


To show replies inside replies, you'll have to do a recursive call to keep on generating the sub replies.

Something like

function get_comments($comment_id) {
    print '<div class="comment_body">';

    // print comment body or something?

    if (comment_has_reply($comment_id)) {
        foreach(comment_comments($comment_id) as $comment) {
            get_comments($comment->id);
        }
    }

    print '</div>';
}

To indent comments however, use css.

<style type="text/css">
.comment_body {
    margin-left:10px;
}
</style>

This way sub replies are indented more than the parent, and their subs are indented even more, and so on.

like image 30
kamasheto Avatar answered Sep 26 '22 10:09

kamasheto