2

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.

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

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

        }
    }
?>

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));
        }
    }
}
?>
ulr
  • 23
  • 6
  • whats your database/table schema? –  Nov 30 '16 at 13:37
  • 5
    add column parent_id in data and when someone "reply" save the parent answer id in data base. When you render it then render it recrusivly so start with parent then child then grand child etc. (tree) – Robert Nov 30 '16 at 13:38

1 Answers1

0

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

See the post about Managing Hierarchical Data in MySQL.

[UPDATE]

Or if you dont care about performance you can follow this topic solution: Nested comments in PHP & MySQL

Community
  • 1
  • 1
rogeriolino
  • 1,095
  • 11
  • 21