如何在不手动添加所有级别的情况下获得更深层次的评论回复?

时间:2017-09-05 14:15:12

标签: php mysql

我试图制作一条评论脚本,对评论做出回复并回复答复,这是我到目前为止所处的地方但如果不手动添加所有支票,我怎么能更深入?

目前看起来像这样:

Top Tier Comment #1
...Reply #1 To Top Tier Comment
......Reply #2 To Top Tier Comment
Top Tier Comment #2
...Reply To Top Tier Comment #2
......Reply to Reply on 1st Comment on #2 Top Tier Comment

这是我目前的代码:

$r_id = 102541;
$stmt = $mysqli->prepare("SELECT cid, comment, datetime, cid_parent, comments.uid, username, picture FROM comments JOIN members ON comments.uid = members.user_id WHERE rid = ?");
$stmt->bind_param('i', $r_id);
$stmt->execute();
$stmt->bind_result($cid, $comment, $datetime, $cidparent, $uid, $commentUsername, $commentPicture);
$stmt->store_result();
$totalComments=$stmt->num_rows;
while ($stmt->fetch()) {
    if(empty($cidparent)) {
        $topTierComments[$cid] = $comment; 
    } else {
        $commentReplys[$cidparent][$cid] = $comment;
    }

}   


$indent = "...";
$indentTwo = "...";
foreach($topTierComments as $topTierCommentID => $topTierComment) {
    echo $topTierComment."<br />";
    if(isset($commentReplys[$topTierCommentID])){
        foreach($commentReplys[$topTierCommentID] as $commentReplyID => $commentReply) {
            echo $indent."".$commentReply."<br />";
                if(isset($commentReplys[$commentReplyID])){
                    foreach($commentReplys[$commentReplyID] as $commentReplyID => $commentReply) {
                        $oldIndentTwo = $indentTwo;
                        $indentTwo .= "...";
                        echo $indentTwo."".$commentReply."<br />";
                        $indentTwo = $oldIndentTwo;
                    }
                }
        }
        $indent .= "...";
    }
    $indent = "...";
}

0 个答案:

没有答案