Reddit Style Comments

To make a php based comment section like the one on Reddit, a structure for comments needs to be set up in both the database and the web page so the comment tree can be built quickly and displayed properly.

For this site, a LAMP server is in use, and the M stands for mySQL so modify as you need based on your flavour of SQL.

Database Setup

For the database, 2 tables are required to run the comments. The comments table holds the actual comment, votes, post id, user id, creation and edit dates, and depth. The childrenComments table contains only the commentID, childID, and postID. The way this works is that if a child comment is added, its parent will be added to the childrenComments table, along with the id of the newly formed child comment. The depth will be recorded on the child as parent+1. The depth is not mandatory, but may be used to speed up searching when it gets too big. 

PHP Setup

Inside the function to get the comments, the db query for the comments needs the depth of the comment as the depth is used to determine which comment is a post comment, and which are replies. Post comments have no parent, and are put directly under the post. Their children are then needed below, and this is a recursive call.

the getChildren function takes the comment passed in, and runs a query to find children comments by joining the comments table and the childrenComments table together. If there is a childrenComments table entry, there is a child. This is then added to the output and a recursive call is done on each child comment.

Code


function getComments($id){
	global $conn;
	$sql = "SELECT comments.id
	, comments.postID
	, users.username
	, COMMENT as comment
	, vote
	, date_created
	, date_edited
	, depth
	FROM comments
	
	LEFT JOIN users ON comments.userID = users.id
	WHERE postID = $id
ORDER BY depth, vote, date_created DESC";
	$result = mysqli_query($conn, $sql);

	// fetch query results as associative array.
	$comments =mysqli_fetch_all($result, MYSQLI_ASSOC);
	
	$post_slug = $_GET['post-slug'];
	$post = getPost($post_slug);
	
	$output = "";
	
	

	foreach($comments as $comment){
		if($comment['depth'] == 0){
			$output .= "
$output.= commentToHTML($comment, $post) .getChildren($comment, $post, 1) ." $output.=" } } //return $comments; return $output; } function getChildren($comment, $post, $depth){ global $conn; $commentID = $comment['id']; $sql = "SELECT comments.id , comments.postID , users.username , COMMENT as comment , vote , date_created , date_edited , depth FROM comments RIGHT JOIN (SELECT * FROM childrenComments WHERE commentID = $commentID) AS cc ON cc.childID = comments.id LEFT JOIN users ON comments.userID = users.id ORDER BY depth, vote ASC, date_created DESC "; $result = mysqli_query($conn, $sql); // fetch query results as associative array. $comments = mysqli_fetch_all($result, MYSQLI_ASSOC); $output = ""; if($comments){ foreach($comments as $subComment){ $output .= "
"; $output.= commentToHTML($subComment, $post) .getChildren($subComment, $post, $depth + 1); $output.="} } return $output; }

Comments

You need to log in to post comments