简体   繁体   English

如何在评论中添加回复功能?

[英]How can I add a reply function to comments?

I've created a comment function to my website. 我已经为网站创建了评论功能。 I would like to add a reply function to it, but I'm not sure how to do it. 我想为其添加回复功能,但是我不确定该怎么做。

I am able to post comments and retrieve them on the website. 我可以发表评论并在网站上检索它们。

I would like to do a reply function which uses it's "parents" id to show up underneath it. 我想做一个回复功能,它使用它的“父母” ID来显示在它的下面。

Desired output: 所需的输出:

  1. First Comment 第一评论
    1. First reply 第一回覆
    2. Second reply 第二次回复
  2. Second Comment 第二条评论
    1. First reply 第一回覆
    2. Second reply 第二次回复

My program looks like this: 我的程序如下所示:

<html>
<form action="" method ="POST">
                            Namn: <br>
                            <input type="text" name ="name"><br>
                            Kommentar: <br>
                            <textarea name="comment" rows="10" cols="20">    </textarea><br>
                            <input type ="submit" name ="submit"   value="Skicka"> 
    </form>
    </html>

connectDB connectDB

<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'com';

$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die(mysqli_error($connect));
       ?>

getComments getComments

<?php
include ('connectDB.php');
if($connect){
        mysqli_select_db($connect, "comments");
        $query2 = "SELECT * FROM data ORDER BY `id` DESC";
        $result = mysqli_query($connect, $query2);

        $comments = array();

        while($row = mysqli_fetch_array($result)){
            $name = $row['Name'];
            $comment = $row['Comment'];
            $date = $row['Date'];

            echo "
                        <div style='width:60%' class='col-lg-12'>
                            <div class='panel panel-default'>
                                <div class='panel-heading'>
                                    <strong> $name </strong><span style='float:right'class='text-muted'>$date</span>
                                </div>
                                <div class='panel-body'>$comment
                                </div>
                            </div><!-- /panel panel-default -->
                        </div><!-- /col-sm-5 -->";

        }
    }
?>

StoreComments StoreComments

<?php
if(isset($_POST['submit'])){
        $name = htmlentities($_POST['name']);
        $comment = htmlentities($_POST['comment']);
        $date = date("Y-m-d");
        $connect = mysqli_connect("localhost", "root", "");

        if($connect){
            mysqli_select_db($connect, "comments");
            $query = "INSERT INTO data(Name, Comment, Date) VALUES (\"" . $name . "\", \"" . $comment . "\", \"" . $date . "\")";

            if(mysqli_query($connect, $query)){

            } else {
                die ("Failed: " . mysqli_error($connect));
            }
        } else {
            die("Failed to connect to mysql: " . mysqli_errno($connect));
        }
    }
}
?>

To prevent the N+1 queries problem, when for each comment you make another one query to find replies, you can use the Nested Set Model strategy, like described in this topic: php / Mysql best tree structure 为防止N + 1查询问题,当您对每个注释进行另一个查询来查找答复时,可以使用嵌套集模型策略,如本主题中所述: php / Mysql最佳树结构

See the post about Managing Hierarchical Data in MySQL . 请参阅有关在MySQL中管理分层数据的文章。

[UPDATE] [UPDATE]

Or if you dont care about performance you can follow this topic solution: Nested comments in PHP & MySQL 或者,如果您不关心性能,则可以遵循以下主题的解决方案: PHP和MySQL中的嵌套注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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