Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Recursive function for nested comments in php

I am trying to display nested comments with php. Each comment has an id and parent_id. The main comments have a parent_id of 0 while the replies have a a parent_id that equals the id of the comment that was replied to. For example if we do comment id:50, we should be calling the recursive function, making the new parent_id 50.When our foreach gets to comment id:58, it should pass the if (50 == 50) and be displayed with an indent. The problem I am experiencing is the recursive function isn’t being called at all, and I’m only seeing the main comments (those with a parent_id of 0).

  if($result) 
  {
    $commentsArr = mysqli_fetch_all($result, MYSQLI_ASSOC);
    echo displayIndentedComments($commentsArr);
  }


  function displayIndentedComments($commArr, $parent_id = 0, $indent = 0) 
  {
    
    $commentHTML = '';
    
    foreach ($commArr as $comment) 
    {

      $username = $comment['username'];
      $timestamp = date("M j, Y, h:i A", strtotime($comment['submit_date']));
      $commentText = $comment['content'];
      $commentId = $comment['id'];
      $commentParentId = $comment['parent_id'];
            
      if($commentParentId == $parent_id) 
      {
        
        $marginLeft = 10 * $indent;
        $marginLeftAttr = $marginLeft . 'px';
        $commentHTML .= "<div id='$commentId' style='margin-top: 30px; margin-bottom: 30px; margin-left: $marginLeftAttr;'>
          <b style='color: LightSeaGreen;' >$username</b> - <span style='font-size: 13px';>$timestamp</span><br>
                
          <div class='container m-0 p-0'  style='max-width: 900px;'>
            <div class='row'>
              <div style='margin: auto;' class='col-9'>
                <div class='mt-1'>$commentText</div> 
            </div>
              <div class='col-3' style='text-align: right;'>
                <button id='replyButton' onclick='showReplyCommentBox(this)' style='background: none; border: none;'><i class='fa fa-reply' aria-hidden='true'></i></button>
              </div>
            </div>
          </div>
                
          <p class='border-bottom mt-2'></p>
        </div>
      ";
      
        displayIndentedComments($commArr, $commentId, $indent + 1);
      }
    }
          
    return $commentHTML;
  }

{"id":"60","page_id":"1","parent_id":"0","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 13:00:01"}
{"id":"59","page_id":"1","parent_id":"0","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 12:58:52"}
{"id":"58","page_id":"1","parent_id":"50","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 12:56:10"}
{"id":"57","page_id":"1","parent_id":"53","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 12:55:50"}
{"id":"56","page_id":"1","parent_id":"0","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 11:49:12"}
{"id":"55","page_id":"1","parent_id":"0","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 11:45:44"}
{"id":"54","page_id":"1","parent_id":"0","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 11:38:11"}
{"id":"53","page_id":"1","parent_id":"0","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 11:37:02"}
{"id":"52","page_id":"1","parent_id":"0","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 11:08:56"}
{"id":"51","page_id":"1","parent_id":"0","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 11:01:50"}
{"id":"50","page_id":"1","parent_id":"0","username":"abc","content":"Lorem ipsum","submit_date":"2023-11-10 10:59:13"}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When you make the recursive call, you’re not doing anything with the result. It needs to be added to the output string.

Change

displayIndentedComments($commArr, $commentId, $indent + 1);

to

$commentHTML .= displayIndentedComments($commArr, $commentId, $indent + 1);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading