Fishcakes Posted November 17, 2021 Share Posted November 17, 2021 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 */ Quote Link to comment https://forums.phpfreaks.com/topic/314241-outputting-an-array-then-cycling-through-another-array-if-the-key-matches-the-key-in-the-original-array/ Share on other sites More sharing options...
maxxd Posted November 17, 2021 Share Posted November 17, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/314241-outputting-an-array-then-cycling-through-another-array-if-the-key-matches-the-key-in-the-original-array/#findComment-1592184 Share on other sites More sharing options...
Fishcakes Posted November 17, 2021 Author Share Posted November 17, 2021 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 } Quote Link to comment https://forums.phpfreaks.com/topic/314241-outputting-an-array-then-cycling-through-another-array-if-the-key-matches-the-key-in-the-original-array/#findComment-1592185 Share on other sites More sharing options...
maxxd Posted November 17, 2021 Share Posted November 17, 2021 The ease and method of doing what you're asking is heavily dependent on the structure of the original data, which is why I asked to see GetPosts(). I assume that's where the query is - that's what we need to see. Quote Link to comment https://forums.phpfreaks.com/topic/314241-outputting-an-array-then-cycling-through-another-array-if-the-key-matches-the-key-in-the-original-array/#findComment-1592204 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.