Jump to content

Trying to output multiple comments


Fishcakes
 Share

Recommended Posts

So I'm trying to create a comment system with the comment SQL table looking like this

describe Posts 
    -> ;
+---------------+-----------------+------+-----+---------------------+-------------------------------+
| Field         | Type            | Null | Key | Default             | Extra                         |
+---------------+-----------------+------+-----+---------------------+-------------------------------+
| id            | int(6) unsigned | NO   | PRI | NULL                | auto_increment                |
| User          | varchar(30)     | NO   |     | NULL                |                               |
| PostTimeStamp | timestamp       | NO   |     | current_timestamp() | on update current_timestamp() |
| CommentText   | varchar(8000)   | YES  |     | NULL                |                               |
| IDOfThread    | int(11)         | YES  |     | NULL                |                               |
| Upvotes       | int(11)         | NO   |     | 0                   |                               |
| ParentId      | int(11)         | YES  |     | NULL                |                               |
| level         | int(11)         | YES  |     | NULL                |          

So I then pull from the SQL table like this - creating an array for each 'level' of the data and how I want the comment to be displayed

while ($row = mysqli_fetch_array($query)) {
    //May need this later to output pictures
    //     $imageURL = 'upload/'.rawurlencode($row["filename"]);
    $CommentText = nl2br($row['CommentText']);
	 $avatarFilePath = $row['avatar'];
	 $id = $row['IDOfThread'];
	 $PostID = $row['id'] ; 
	 $ParentId = $row['ParentId']; 
    convertYoutube($CommentText);
    //Work out Margin for comment replies 
	$Level = $row['level'] * 75; // Used to multiply the margin to create nested comments  
	echo $Level ; 
	 //$Level = 1 * 75 ;  
	 $margin = 	"<div class='divTableCell' style='margin-left: $Level" . "px ; '>"; //input the margin in child comments
	 //$margin = 	"<div class='divTableCell' style='margin-left: 75" . "px ; '>"; //input the margin in child comments
    
    

    $ParentComment = "";
//Get parent comments into an array 
    if (empty($row['ParentId'])) {
           $ParentComments[$PostID] = "  <div class='divTableRow'>
	<div class='divTableCell'>
		<div class ='UserAndAvatarCommentBox'>
<div > <img src=$avatarFilePath alt='' />	</div> 
	<div class='profileUsername'> {$row['User']} </div> 
</div>
		<div class='pointsincommentbox'> {$row['Upvotes']}points</div>

		<div class='divTableComment'> 
		 $CommentText <br>
		 <div> 
			<div class='divCommentLinks'> 
		 <button type='button'></button> 
		<button type='button'></button> 
		<div> $PostID </div> 
		<button type='button'> view comment </button> 
		<button type='button'>report </button> 
		<button type='button'>permalink</button> 
		<button type='button' class ='CommentChildButton'>reply</button>
		<div class ='OpenChildCommentBox'> 
		
				<form action='CommentUpload.php' method='post' enctype='multipart/form-data'>
				<table>
				<tr>
				<td></td>
				</tr>
				<input type='text' value=$PostID name='PostId' />
				<input type='text' value='1' name='level' />
				<tr>
				<td>Comment: </td>
				<td> <textarea name='CommentText' cols='100' rows='10' > Enter your posts... 
				</textarea>
				 </td>
				<td></td>
				</tr>
				<tr>
				<td></td>
				<td><input type='submit' name='submit' value='Submit'/></td>
				<td></td>
				</tr>
				</table> 
				</form>
		
		  	
		</div> 
		</div> 
	</div>

 </div>

</div>
</div> 
    \n";
    }
   //Get child comments into an array level 1
       if ($row['ParentId'] && $row['level'] == 1 ) {
           $replies[$ParentId] = "  <div class='divTableRow'>
		<div class='divTableCell' style='margin-left:75px'>
		<div class ='UserAndAvatarCommentBox'>
<div > <img src=$avatarFilePath alt='' />	</div> 
	<div class='profileUsername'> {$row['User']} </div> 
</div>
		<div class='pointsincommentbox'> {$row['Upvotes']}points</div>

		<div class='divTableComment'> 
		 $CommentText <br>
			<div class='divCommentLinks'> 
		 <button type='button'></button> 
		<button type='button'></button> 
		<div> $PostID </div> 
		<button type='button'> view comment </button> 
		<button type='button'>report </button> 
		<button type='button'>permalink</button> 
		<button type='button' class ='CommentChildButton'>reply</button>
		<div class ='OpenChildCommentBox'> 
		
				<form action='CommentUpload.php' method='post' enctype='multipart/form-data'>
				<table>
				<tr>
				<td></td>
				</tr>
				<input type='text' value=$PostID name='PostId' />
				<input type='text' value={$row['level']} name='level' />
				<tr>
				<td>Comment: </td>
				<td> <textarea name='CommentText' cols='100' rows='10' > Enter your posts... 
				</textarea>
				 </td>
				<td></td>
				</tr>
				<tr>
				<td></td>
				<td><input type='submit' name='submit' value='Submit'/></td>
				<td></td>
				</tr>
				</table> 
				</form>
		
		
		 	
		</div> 
	</div>

 </div>
</div>
</div>
</div> 
    \n";
    }
  
//Get child comments into an array level 2
       if ($row['ParentId'] && $row['level'] == 2 ) {
           $Level2[$ParentId] = " 
		$margin
			<div class ='UserAndAvatarCommentBox'>
<div > <img src=$avatarFilePath alt='' />	</div> 
	<div class='profileUsername'> {$row['User']} </div> 
</div>
		<div class='pointsincommentbox'> {$row['Upvotes']}points</div>

		<div class='divTableComment'> 
		 $CommentText <br>
			<div class='divCommentLinks'> 
		 <button type='button'></button> 
		<button type='button'></button> 
		<div> $PostID </div> 
		<button type='button'> view comment </button> 
		<button type='button'>report </button> 
		<button type='button'>permalink</button> 
		<button type='button' class ='CommentChildButton'>reply</button>
		<div class ='OpenChildCommentBox'> 
		
				<form action='ChildCommentUpload.php' method='post' enctype='multipart/form-data'>
				<table>
				<tr>
				<td></td>
				</tr>
				<input type='text' value=$PostID name='PostId' />
				<input type='text' value={$row['level']} name='Level' />
				<tr>
				<td>Comment: </td>
				<td> <textarea name='CommentText' cols='100' rows='10' > Enter your posts... 
				</textarea>
				 </td>
				<td></td>
				</tr>
				<tr>
				<td></td>
				<td><input type='submit' name='submit' value='Submit'/></td>
				<td></td>
				</tr>
				</table> 
				</form>
		
		
		 	
		</div> 
	</div>

 </div>
</div>
</div>
</div> 
    \n";
    }
    
  

    }

Then after the data has been put into arrays I then output it using this foreach statement

foreach ($ParentComments as $key => $reply)
{
 	echo $reply  ;

	foreach ($replies as $childKey => $childReply)
	{ 
	if ($key == $childKey)
	{ 
	echo $childReply ; 
		foreach ($Level2 as $Key2 => $Level2Reply)
		{ 
		if ($childKey == $Key2) 
			{
				echo $Level2Reply ; 
 			} 
		
		}
	}
	}
}

So the problem I'm having is if you have a top level comment like

TopLevelComment

Then respond to it you get

TopLevelComment

-Level1ReplyComment

However if you respond again to the top level comment it only shows the new comment like

TopLevelComment

-NewLevel1Comment

Why would it do this rather than add another comment beneath the top level comment?

Link to comment
Share on other sites

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.

 Share

×
×
  • 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.