脚本宝典收集整理的这篇文章主要介绍了php – 显示评论和回复?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
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 |
我写的这段代码只是为了显示评论,但我希望评论能够显示评论的回复(如果有的话).在它显示下一个评论之前
<?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>'; } } ?>
<?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; } ?>
将上面的代码放在视图文件的顶部.现在使用以下行显示/回显您的评论和回复.
<?PHP echo print_comments( Yii::$app->getRequest()->getQueryParam('id'),0); ?>
(上一个答案)
您可以尝试遵循此伪代码:
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> }
优点:更易于理解和实施.
缺点:使用大量查询.
任何其他方式来克服利弊?
请记住,当与分页一起使用时,这种做法很难实现.
以上是脚本宝典为你收集整理的php – 显示评论和回复?全部内容,希望文章能够帮你解决php – 显示评论和回复?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。