javascript - 如何给评论添加回复功能?

标签 javascript php html json ajax

我在我的网站上创建了一个评论功能。我想给它添加一个回复功能,但我不知道该怎么做。

我可以发表评论并在网站上检索它们。

我想做一个回复功能,它使用它的“ parent ”ID 显示在它下面。

Desired output:

  1. First Comment
    1. First reply
    2. Second reply
  2. Second Comment
    1. First reply
    2. Second reply

我的程序是这样的:

<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>

连接数据库

<?php

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

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

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 -->";

        }
    }
?>

商店评论

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

最佳答案

为防止 N+1 查询问题,当您针对每个评论进行另一个查询以查找回复时,您可以使用嵌套集模型策略,如本主题所述:php / Mysql best tree structure

查看关于 Managing Hierarchical Data in MySQL 的帖子.

[更新]

或者如果您不关心性能,您可以按照此主题解决方案:Nested comments in PHP & MySQL

关于javascript - 如何给评论添加回复功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40889792/

相关文章:

html - 制作响应式工具栏 Bootstrap 4

javascript - 有人可以解释为什么 document ["write"] 有效吗?

javascript - 我可以在 ngView 中使用哪些属性来显示当前路由的模板?

php - 将表单中 "on"的所有值更改为 "Yes"

php - 在没有 AUTO_INCREMENT 的 CHAR 字段中查找下一个 ID

jquery 更改/切换?

javascript - InputMask 范围从 0 到 254

javascript - 什么脚本与 HTML 的哪一部分配合?

php - 从客户我的帐户页面删除已取消的 WooCommerce 订单

javascript - HTML 元素作为 While 条件