简体   繁体   中英

How to loop over the query results of a comments system with replies?

I am trying to display comments from database. Some comments are children of other comments (parents), so in my database I put a field of parent with null as default value, if the new comment is a child comment I insert the id of the parent comment in that field.

Insertion works fine but I am facing issues with displaying system. I try to display every child comment under its paren, with my code parent element are displayed but children remains empty.

I have tried with the print_r function to see if there is data in the array, it shows data, I think i am missing something or I'm having some logical error in my code construction.

if(is_array($this->comments))
{ 
    $comments = array();
    $parent_array = array();
?>
    <ol>
    <?php   
    foreach($this->comments as $key => $value) {
        if($value['parent'] === 0) {
            $comments[] = $value;
        } else {
            $parent_array['parent'][] = $value;
            $comments[] = $parent_array;
        }
    }
    foreach($comments as $key => $value) {
    ?>
    <div> 
        <li style="color: #dc143c; font-size: 14pt;margin-left: 20px;">
        - <?php echo $value['id']; ?>
        </li>
        <p> <?php echo $value['title'] ; ?></p>
        <p> <?php echo $value['description'] ; ?></p>

    <?php 
    }
}
?>

Looked at your code, I don't think you are grouping your comments and sub-comments well. Try this

<?php    
       $all_comments = [];

       foreach($this->comments as $key => $value){
          if($value['parent'] === 0) 
          {
            $all_comments[$value['id']]['comment'] = $value;
          } else {
            $all_comments[$value['parent']]['subcomment'][] = $value;
          }
       }

       foreach($all_comments as $comment){
          // display only main comments here
          if(isset($comment['comment'])) {
            ?>
            <div> 
            <li style="color: #dc143c; font-size: 14pt;margin-left: 20px;">
            - <?php echo $comment['comment']['id']; ?>
            </li>
            <p> <?php echo $comment['comment']['title'] ; ?></p>
            <p> <?php echo $comment['comment']['description'] ; ?></p>

            <!-- All its sub-comments -->
            <?php
               if(isset($comment['subcomment'])) {
                   displaySubComment($all_comments, $comment);
               }
          }
     }

     function displaySubComment($all_comments, $comment) 
     {
       foreach($comment['subcomment'] as $subcomment) {
          ?>
          <h3> Sub-Comments </h3>
          <p> <?php echo $subcomment['title'] ; ?></p>
          <p> <?php echo $subcomment['description'] ; ?></p>
          <?php
          // re-iterate over itself, if more comments exist under a comment and display all
          if(array_key_exists($subcomment['id'], $all_comments)) {
             displaySubComment($all_comments, $all_comments[$subcomment['id']])
          }                
       }
     }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM