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:
- First Comment
- First reply
- Second reply
- Second Comment
- First reply
- 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));
}
}
}
?>