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:!2/611e2d/5

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.