iamLearning Posted May 16, 2013 Share Posted May 16, 2013 I am creating a comment system which also displays replies. I am wanting to display the reply underneath the parent comment. As of right now, my code is functional but I can not think of a logical way to list the reply under-neath the parent comment. Here is my code. The code I wrote here is 100% functional. It is even able to sort through the comments and figure out which is a reply and which is not. The problem is I just don't know how to group the replies with the parent comment. If $parent_id = $comment_id then I know that $comment_id is the comment to display the reply underneath, but how would I write than in code? <?php //Retrieve Comments From Database $comments = mysql_query("SELECT id,parentid,commentpage,user,comment,avatar FROM comments WHERE commentpage='$youtubeid' ") or die(mysql_error()); if (!$comments) { //Check to see if there are any comments for this video. exit; }else{ while ($commentrow = mysql_fetch_row($comments)){ //If there are comments, then let's get their information. $comment_id = $commentrow['0']; //Get Comment ID $parent_id = $commentrow['1']; //Check to see if this comment is a reply. $comment_pageid = $commentrow['2']; //Get Comment Page ID $comment_author = $commentrow['3']; //Get Comment Author $comment_message = $commentrow['4']; //Get Comment Author $comment_avatar = $commentrow['5']; //Get Comment Avatar //Loop All Comments Here if ($parent_id == 0){ //Check to see if this is a reply or not. echo $comment_author.' commented <b>'.$comment_message.'</b><br>'; }else { // How do we handle the replies? echo $comment_message.' is a reply'; } //End Loop All Comments Here } } ?> Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted May 16, 2013 Solution Share Posted May 16, 2013 try something like this $sql = "SELECT c.id, c.commentpage, c.user, c.comment,c.avatar, r.user, r.comment FROM comments c LEFT JOIN comments r ON c.id = r.parent_id WHERE c.commentpage='$youtubeid' ORDER BY c.id"; $res = mysql_query($sql); $currenComment=''; while (list($id, $page, $c_user, $c_comment, $avatar, $r_user, $r_comment) = mysql_fetch_row($res)) { if ($currenComment != $id) { echo "<p class='comment'>$c_user<br>$c_comment</p>"; $currenComment = $id; } echo "<p class='reply'>$r_user<br>$r_comment</p>"; } Quote Link to comment Share on other sites More sharing options...
iamLearning Posted May 16, 2013 Author Share Posted May 16, 2013 (edited) Thanks for the advice! I'll give it a try! -edit- I have a quick question. In the above code you used the following: sql = "SELECT c.id, c.commentpage, c.user, c.comment,c.avatar, Are you concatenating those fields or are you suggesting I rename the fields to this? I wasn't aware that you could concatenate within an MYSQL query like that. Edited May 16, 2013 by iamLearning Quote Link to comment Share on other sites More sharing options...
iamLearning Posted May 16, 2013 Author Share Posted May 16, 2013 Hey the code works! Thanks for helping, it's just I wish I understood that method earlier. I need to read more about MYSQL queries. Thanks again. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 16, 2013 Share Posted May 16, 2013 Thanks for the advice! I'll give it a try! -edit- I have a quick question. In the above code you used the following: sql = "SELECT c.id, c.commentpage, c.user, c.comment,c.avatar, Are you concatenating those fields or are you suggesting I rename the fields to this? I wasn't aware that you could concatenate within an MYSQL query like that. No, just specifying which fields are selected from which comments table. We are joining the comments table to the comments table so they need different aliases. Logically, there is a comment table (alias c) and a reply table (alias r). Concatenation is done with the CONCAT() or CONCAT_WS() functions Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.