选择属于MYSQL中父注释的所有回复

时间:2016-12-25 06:14:28

标签: php mysql comments

我知道有很多类似的问题,但是从昨天开始搜索之后,我只想出了与单个表有关的问题。

现在我有两个表,即评论 comment_replies ,一个用于评论,一个用于回复,其结构如下,并附有一些示例数据:

评论栏

comment_id | blog_id | comment_content | comment_time | comment_by
1          | 2       | parent comment  | 13:33:17     | user1
2          | 2       | parent comment  | 13:34:17     | user2

COMMENT_REPLIES FIELD

c_reply_id | parent_comment_id | blog_id | reply_content | reply_time | comment_by
1          | 1                 | 2       | first reply   | 13:35:17   | user3
2          | 1                 | 2       | second reply  | 13:36:17   | user1

我想获取所有回复的所有父评论。我目前使用的查询是这样做的,除非它只检索一个回复,即使父评论有多个。

当前查询

$getcomments =  mysqli_query($db_connect, "SELECT * FROM comments WHERE blog_id = '$blog_id' ORDER BY comment_id ASC" );
$getcommentscount = mysqli_num_rows($getcomments);


    if($getcommentscount != 0 ) {
        while($comment = mysqli_fetch_assoc($getcomments)){

            $comment_id = $comment['comment_id']; 
            $comment_content = $comment['comment_content']; 
            $comment_time = $comment['comment_time'];
            $comment_by = $comment['comment_by'];

            //Get picture of owner of comment
            $get_user_info = mysqli_query($db_connect, "SELECT * FROM users WHERE username='$comment_by'");
            $get_info = mysqli_fetch_assoc($get_user_info);
            $commenter_image = $get_info['picture'];
            $commenter_fname = $get_info['fname'];
            $commenter_lname = $get_info['lname'];

            if ($commenter_image == "") {

                $commenter_image = "defaultp.jpg";

            }



            $comment_time_title = date('l, jS F, Y', strtotime($comment_time));
            $comment_time_title = $comment_time_title." at ". date('g:ia', strtotime($comment_time));



            $getreplies =  mysqli_query($db_connect, "SELECT * FROM comment_replies WHERE parent_comment_id = '$comment_id' ORDER BY c_reply_id DESC" );
            $getgetreplies_info = mysqli_num_rows($getreplies);

            while ($reply = mysqli_fetch_array($getreplies)) {

                $c_reply_id = $reply['c_reply_id'];
                $parent_comment_id = $reply['parent_comment_id']; 
                $reply_content = $reply['reply_content']; 
                $reply_time = $reply['reply_time'];
                $reply_by = $reply['reply_by'];


                $reply_time_title = date('l, jS F, Y', strtotime($reply_time));
                $reply_time_title = $reply_time_title." at ". date('g:ia', strtotime($reply_time));

                //Get picture of owner of comment
                $getreplyer = mysqli_query($db_connect, "SELECT * FROM users WHERE username ='$reply_by'");
                $getreplyer_info = mysqli_fetch_assoc($getreplyer);
                $replyer_image = $getreplyer_info['picture'];
                $replyer_fname = $getreplyer_info['fname'];
                $replyer_lname = $getreplyer_info['lname'];

                if ($replyer_image == "") {

                    $replyer_image = "defaultp.jpg";

                }



            }

            if($getgetreplies_info >= 1){
                $displayReply = '
                                <div id="singleComment'.$c_reply_id.'" class="singleReplyComment clearfix">
                                    <div class="sComment-Box clearfix">
                                        <div class="cUser-image clearfix">
                                            <img src="'.$base_url.'images/userdata/profileimage/'.$replyer_image.'">
                                        </div>
                                        <div class="cDetails clearfix">
                                            <div class="cUser-name clearfix"><a href="'.$base_url.'user/'.$reply_by.'">'.$replyer_fname.' '.$replyer_lname.'</a></div>
                                            <div class="commentContent">'.$reply_content.'</div>
                                        </div>
                                        <div class="cPosted-on clearfix"><time class="timeago" datetime="'.$reply_time.'" title="'.$reply_time_title.'">an hour ago</time></div>
                                    </div>
                                </div>';
            } else {
                $displayReply = '';
            }

                echo '

                    <div id="singleComment'.$comment_id.'" class="singleComment clearfix">
                        <div class="sComment-Box clearfix">
                            <div class="cUser-image clearfix">
                                                            <img src="'.$base_url.'images/userdata/profileimage/'.$commenter_image.'">
                            </div>
                            <div class="cDetails clearfix">
                                <div class="cUser-name clearfix"><a href="'.$base_url.'user/'.$comment_by.'">'.$commenter_fname.' '.$commenter_lname.'</a></div>
                                <div id="cReply_id'.$comment_id.'" class="cReply clearfix">Reply</div>
                                <div class="commentContent">'.$comment_content.'</div>
                            </div>
                            <div class="cPosted-on clearfix"><time class="timeago" datetime="'.$comment_time.'" title="'.$comment_time_title.'">'.$comment_time.'</time></div>
                            <div id="replyCommentcReply_id'.$comment_id.'" class="replyComment clearfix">
                                <form id="replyComment-form'.$comment_id.'" class="replyComment-form">
                                    <textarea class="commentText" name="userComment"></textarea>
                                    <div class="error commenterror"></div>
                                    <input type="submit" class="postComment-butt replyCom-butt" value="Post">
                                </form>
                            </div>
                        </div>
                        '.$displayReply.'
                    </div>

                ';
        }
    } else {
        echo "<center><br><br><br>No comments to display!</center>";
    }

以上查询有效,但就像我说它只检索父评论的一个回复,即使父评论有多个回复。

注意:请注意,回复无法回复,但父评论可以包含尽可能多的回复。

提前致谢!

0 个答案:

没有答案