Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示评论和回复?_Php_Mysql_Yii2 - Fatal编程技术网

Php 显示评论和回复?

Php 显示评论和回复?,php,mysql,yii2,Php,Mysql,Yii2,我试图显示评论和回复,但我真的不知道怎么做。这是我的桌子 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 re

我试图显示评论和回复,但我真的不知道怎么做。这是我的桌子

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>';
    }
  }
?>  

我对Yii不熟悉。但是您可以使用递归来实现这一点。尝试执行类似于以下内容的操作:

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();
函数显示回复($parent\u id=0,$level=0){
$query=“从注释中选择*,其中parent\u id=$parent\u id”;
$result=mysql\u query($query);
if(!$result | |!mysql_num_rows($result)){
返回;
}
而($detail=mysql\u fetch\u数组($result)){
$space='';
对于($i=0;$i<$level;$i++){
$space.=“--”;
}
echo$space.“国家名称:”.$detail['user_comment']”。
; echo$space.“状态名称:”.$detail['byy'].
”; echo$space.“城市名称:”.$detail['Name']”。
; 显示回复($detail['comment\u id',$level+1); } } 显示回复();
以下是伪代码的实际代码:

<?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;
}
?>
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>
}
优点:易于理解和实施。
缺点:使用大量查询

还有其他克服缺点的方法吗??
请记住,当与分页一起使用时,这种方法更难实现

  • 使用单个查询获取所有评论和回复
  • 将它们全部格式化为comment=>replies关系
  • 循环浏览关系并显示它

  • 对于评论回复,是否存在任何最大深度级别,或者它将是无限的?它应该是可扩展的MySQL\u查询已被弃用,所以我使用了mysqli。这也说明了数据类型的问题所在。修复了itnew“尝试获取非对象属性时出错”。它将我引向$detail->id lineI,我最初在示例代码中使用了
    mysql\u fetch\u object
    。忘了换那行了。刚刚修好了。请检查。我用一些样本数据在我的计算机上测试了代码,它似乎工作正常
    mysqli_connect():(08004/1040):当您为每个脚本打开多个连接时,会显示太多连接。你正在从你的脚本打开多个mysql连接吗?你不明白我的问题我已经为之前的伪代码添加了实际的代码。我想给你的答案和@user3360140给你的答案是一样的。顺便说一句,我觉得自己太愚蠢了。你的回答是对的。如何使用css使回复稍微向右移动。例如,所有
    ul-li
    都是一级评论,
    ul-li
    都是对一级评论的回复,
    ul-li
    都是对回复的回复,等等。
    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>
    }