Show comment and answers?

I am trying to show a comment and answers, but I really don't know how to do this. This is my table.

comment_id | byy  | user_comment   | topic_id    | parent_id | 
   1       |obi   |comment 1       |    1        |    0      |
   2       |chima |comment 2       |    1        |    0      |
   3       |eze   |comment 1 reply |    1        |    1      |
   4       |david |comment 2 reply |    1        |    2      |

This code I wrote is just for displaying only the comment, but I want the comment to show the comments of the comment, if any. Before displaying the next comment

<?php
  $querycomment = comment::find()->where(['topic_id'=> Yii::$app->getRequest()->getQueryParam('id')])->all();

  foreach ($querycomment as $detail) {
    if($detail['parent_id']==0) {
      echo 'Country Name: '.$detail['user_comment'].'</br>';
      echo 'State Name: '.$detail['byy'].'</br>';
      echo 'City Name: '.$detail['name'].'</br>';
      echo '</br>';
    }
  }
?>  
+4
source share
2 answers

Here is the actual code for the following pseudocode:

<?php 
// print comments and/or replies body
function print_comments( $topic_id, $parent_id ) {
   $all_comments = Comment::find()
               ->where(
                   'topic_id' => $topic_id,
                   'parent_id' => $parent_id
               )->all();

    if( empty($all_comment) ) {
      return "";
    }

    $comments = '<ul>';
    foreach( $all_comments as $comment ) {
        $comments .= '<li>
            <p> '.$comment->user_comment.' </p>
            <p> by: '.$comment->byy.' </p>';

            // print replies
            $comments .= print_comments( $topic_id, $comment->comment_id ); // recursive

        $comments .= '</li>';
    }
    $comments .= '</ul>';

    return $comments;
}
?>

Put the above code at the top of your view file. Now use the following line in which you want to display / echo your comments and answers.

<?php echo print_comments( Yii::$app->getRequest()->getQueryParam('id'), 0); ?>

(previous answer)

You can try to follow this pseudocode:

print_comments( queryParam(id),  0); // parent_id = 0

// print comments and/or replies body
print_comments ( $topic_id, $parent_id ) {
   $all_comments = Comment::find()
               ->where(
                   topic_id => $topic_id,
                   parent_id => $parent_id
               )->all();

    if( $all_comment count = zero )
      return

    <ul>
    foreach( $all_comments as $comment ) {
        <li>
            <p> $comment->user_comment </p>
            <p> by: $comment->byy </p>

            // print replies
            print_comments( $topic_id, $comment->comment_id ); // recursive

        </li>
    }
    </ul>
}

: .
: .

, .

  • = >
0

Yii. , . sometihng, :

function show_replies($parent_id = 0, $level = 0) {
    $query = "select * from comments where parent_id = $parent_id";
    $result = mysql_query($query);
    if (!$result || !mysql_num_rows($result)) {
        return;
    }

    while($detail = mysql_fetch_array($result)) {
        $space ='';
        for ($i = 0; $i < $level; $i++) {
            $space .= "--";
        }

        echo $space.'Country Name: '.$detail['user_comment'].'</br>';
        echo $space.'State Name: '.$detail['byy'].'</br>';
        echo $space.'City Name: '.$detail['name'].'</br>';
        show_replies($detail['comment_id'], $level + 1);
    }
}

show_replies();
+3

Source: https://habr.com/ru/post/1655094/


All Articles