1

I'm trying to make a comment script that has reply's to the comments and reply's to the reply's, This is where i've got so far which works but how can i go even deeper without manually adding all of the checks?

It looks like this currently:

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

Here is my current code:

$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 = "...";
}
Exoon
  • 1,513
  • 4
  • 20
  • 35
  • You need to do this with a recursive iterator function. – Rasclatt Sep 05 '17 at 14:29
  • see: http://slides.com/philipbrown/deck and https://github.com/philipbrown/ruling-the-hierarchy and http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ and https://stackoverflow.com/q/20215744/4233593 – Jeff Puckett Sep 05 '17 at 18:15

0 Answers0