评论与回复php代码,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 = '

  • ';

foreach( $all_comments as $comment ) {

$comments .= '

'.$comment->user_comment.'

by: '.$comment->byy.'

';

// print replies

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

$comments .= '

';

}

$comments .= '

';

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

foreach( $all_comments as $comment ) {

$comment->user_comment

by: $comment->byy

// print replies

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

}

}

优点:更易于理解和实施.

缺点:使用大量查询.

任何其他方式来克服利弊?

请记住,当与分页一起使用时,这种做法很难实现.

>使用单个查询获取所有评论和回复>将所有这些格式化为comment =>回复关系>循环关系并显示它

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值