Jump to content

Outputting an array then cycling through another array if the key matches the key in the original array


Fishcakes

Recommended Posts

So I take into an array from SQL like so

public function ShowComments($Number) { 
		$datas = $this->GetPosts($Number) ;
		echo "type equals: " . gettype($datas) . "<P>";
		foreach ($datas as $data)
		{ 
	  //May need this later to output pictures
    //     $imageURL = 'upload/'.rawurlencode($row["filename"]);
     $CommentText= nl2br($data['CommentText']);
	 $avatarFilePath = $data['avatar'];
	 $id = $data['IDOfThread'];
	 $PostID = $data['id'] ; 
	 $ParentId = $data['ParentId']; 
    convertYoutube($CommentText);
    
   $oFlairs = new cFlairs();
	$oFlairs->DisplayFlairs($CommentText); 
    
    //Work out Margin for comment replies 
   $levelNumber = $data['level'];
	$Level = $data['level'] * 75; // Used to multiply the margin to create nested comments  
	 //$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($data['ParentId'])) {
           $ParentComments[$PostID] = "  <div class='divTabledata'>
	<div class='divTableCell'>
		<div class ='UserAndAvatarCommentBox'>
<div > <img src=$avatarFilePath alt='' />	</div> 
	<div class='profileUsername'> {$data['User']} </div> 
</div>
		<div class='pointsincommentbox'> {$data['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' datas='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";
    }

 

Above I use the $ParentComments[$PostID] = " etc etc" to build the top level comment

I then take the Level 1 comments which are all the comments that are a reply to a top level comment

Ie.

ParentComment

-ResponseComment

 //Get child comments into an array level 1
       if ($data['ParentId'] && $data['level'] == 1 ) {
           $replies[$ParentId] = "  <div class='divTabledata'>
		<div class='divTableCell' style='margin-left:75px'>
		<div class ='UserAndAvatarCommentBox'>
<div > <img src=$avatarFilePath alt='' />	</div> 
	<div class='profileUsername'> {$data['User']} </div> 
</div>
		<div class='pointsincommentbox'> {$data['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={$data['level']} name='level' />
				<tr>
				<td>Comment: </td>
				<td> <textarea name='CommentText' cols='100' datas='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";
    }

I tried outputting these like the following however it seems to output only 1 of the child comments under the correct Parent comment (the other comments in response to that comment don't show). Is there a better way to do this?

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

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

 

Link to comment
Share on other sites

That's kind of just a wall of poorly formatted code, so it's hard to tell where the actual problem is but let us see the GetPosts() method. What you're describing should be a data gathering concern, not a display concern.

Other unsolicited advice - your entire comment output should be a method of it's own; right now the two blocks are almost identical. And I'm assuming the poor formatting is due to copy/paste issues, but on the off chance that it's not then fix that as it'll make reading your code much easier.

Link to comment
Share on other sites

OK I'll have a go at cleaning it into a function I can call but essentially I am taking in a row of data into an array with also has an array KEY (Which i link to the "id" of the comment)

$ParentComments[$PostID] = "  

I then take in the level 1 comments (ie. replies to a top level comment) with a variable $ParentId for the array key which will take the "ParentId" which should match the "$PostId" variable above.

$replies[$ParentId] = " 

So now I have the top level comments in $ParentComments and I have the responses to these top level comments in $replies

I now want to write a foreach loop which spits out the top level comments like so but after it echos that comment I want to

  • Check if the $replies array contains a response by matching the $ParentComments[$PostId] against the $replies[$ParentId
foreach ($ParentComments as $key => $toplevelcomment)
{
 	echo $toplevelcomment  ;
 	
 	//It is this point that I would now like to check the $replies array
 	//Then if the $key matches the array key of $replies array
 	//echo that comment
 	
 	
 	} 
 	

 

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.