Jump to content

Just need advice please.


iamLearning
Go to solution Solved by Barand,

Recommended Posts

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
	}
	}

?>
Link to comment
Share on other sites

  • Solution

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>";
}
Link to comment
Share on other sites

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 by iamLearning
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.