• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Threaded Responses PHP MySQL

I have a forum that students are posting in. If the student responds to the main post, the postId is set to 0. If a student replies to another student's post, the postId is set to the replyId of the student's original post.

I am trying to write some PHP that will essentially create a new table for each post, with the exception of if a postid is set to a reply Id, then create a new row in that table.

I have the SQL laid out in SQLFiddle which can be found here:


Using this example, what I'm looking for is replyid one to be put in a new html table, with response ID 3 in a new row underneath that. Then with ReplyId 2, would create a new html table.

This is just a basic threaded forum view of responses.

Right now, it is creating a new table for everything rather than grouping where a postId is equal to a replyId.

$getResponses = mysql_query("SELECT replyId, topicId, postId, replyText, userId, responseDate FROM forumResponses WHERE topicId = '".$topicId."' ORDER BY postId, replyId ASC") or die("Cannot pull topic responses: " . mysql_error());

$i = 0;
$aR = 0;
$currentPost = '';

	if(mysql_num_rows($getResponses) > 0)
		while($respData = mysql_fetch_array($getResponses))

			if(($respData['postId'] != $currentPost)) 
					echo '</table><br /><br />';			

				echo '<table width = "875px" cellspacing = "0" cellpadding = "0" border = "0">';

		$currentPost = $respData['postId'];

		$color_A = 'class="altRow1"'; 
		$color_B = 'class="altRow2"';

		$altRowColor = ($aR % 2) ? $color_A : $color_B;
		$studentName = getStudents($respData['userId']);
		$studentName = explode(" ", $studentName);
		$studentFirstName = $studentName[0];
		echo '<tr ' . $altRowColor . '>
				<td align="center" width = "225px" class="forumTopic"><img src="images/'.getStudentPics($respData['userId']).'.png" /><br />Posted By ' . getStudents($respData['userId']) . '<br />on '.date("m/d/Y h:i a", strtotime($respData['responseDate'])) . '</td>
				<td width = "650px" class="forumTopic">' . $respData['replyText'] . '</td>
			<tr ' . $altRowColor . '>
				<td class="forumTopic" colspan = "2" align="center"><span class="topicLinkStyle"><a href="postResponse.php?postId='.$respData['replyId'].'&topic='.$topicId.'" class="iframe750x600">Reply to '.$studentFirstName .'</a></span></td>
	echo '</table><br /><br />';

Open in new window

  • 4
1 Solution
You really can't have a new table for each post. You will soon reach limits as that's not how relational databases are intended to work. All of the posts belong in a posts table. It will be the fields in this table that determine the relationships between the posts. For instance:

postId postText                  replyToId
1          first post                  0
2          reply to postId 1      1
3         reply to postId 1       1
4         second post               0
5         reply to postId4         4
6         reply to postId4         4
t3chguyAuthor Commented:
I meant a new html table for each post, not physical mysql table, sorry.  I do have all post responses held in one table.
ha sorry! I will read the code then.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

I'm not understand the relationship between a "first" post in a topic and a topic. What is the point of topicId?
never mind ...
Basically then it seems like $respData['postId'] is never equal to $currentPost, so it is always createding a new table.

Something to try, I don't know why but I have run into situations where

if($x != $y) doesn't evaluate as expected

if(!($x == $y)) does evaluate correctly.

try replacing line 13 in your code block above with

if(!($respData['postId'] == $currentPost)) 

Open in new window

if you are still having the problem, then print those values to the screen and see if they really are equal or not.

replace line 17 in your code block above with
echo '</table><br />' . $respData['postId'] . ':' . $currentPost . '<br />';

Open in new window

it should display the respdata postId with what you believe the $currentPost id is between the tables that are being displayed.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now