Want to be able to reply to the comments1

Hi, i want be able to reply to the comments.

<?php

session_start();

require_once 'db_conn.php';

if(!isset($_SESSION['user'])){

header("location: login.php?ejlogin");

exit;

}else{

$username = $_SESSION['user'];

}

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Welcome</title>

<link rel="stylesheet" type="text/css" href="welcome.css">

</head>

<body>

<main>

    <section class='userInput'>

        <section class="loginInfo">

            <p>Du är inloggad som: <strong><?php echo $username ?></strong></p>

            <div>

                <button><a href="login.php?logout">Logga ut</a></button><br><br>

            </div>

        </section>

       

        <section class="messageArea">

            <form action="" method="post">

                <label for="comment">Meddelande:</label><br>

                <textarea name="comment" id="" cols="30" rows="10" required></textarea>

                <button type="submit" name="submit">Skicka</button>

            </form>

        </section>

        </section class='messages'>

        <?php

if(isset($_POST['submit'])){

    $comments = $_POST['comment'];

    $sql = "INSERT INTO comments (comment, user, date) VALUES ('$comments', '$username', NOW())";

    $query = mysqli_query($conn, $sql) or die('Det gick inte att ansluta till databasen');

}

            $sql = "SELECT user, comment, date FROM comments ORDER BY date DESC";

            $query = mysqli_query($conn, $sql);

            foreach($query as $userComments){

                extract($userComments);

                echo    "<section>

                            <header>$user<h5>$date</h5></header>

                            <p>$comment</p>

                        </section>";

                       

            }

            mysqli_close($conn);

        ?>

    </section>

</main>

</body>

</html>

Implementing a reply functionality to comments requires a bit of restructuring of your database and code to support comment threading. Here’s a general guide on how you could achieve this:

Step 1: Modify Database Structure

First, you need to modify the comments table to allow replies. A simple way to do this is by adding a parent comment ID to each comment using sql.

ALTER TABLE comments ADD parent_id INT DEFAULT 0;

Here, parent_id will be 0 for top-level comments and the comment ID of the parent comment for replies.

Step 2: Add Reply Form

You can add a reply form beneath each comment. In your PHP code where you are looping through comments, add a form like this:

echo    "<section>
            <header>$user<h5>$date</h5></header>
            <p>$comment</p>
            <form action='' method='post'>
                <input type='hidden' name='parent_id' value='$id'>
                <textarea name='reply_comment' required></textarea>
                <button type='submit' name='reply_submit'>Reply</button>
            </form>
        </section>";

Make sure you have the comment ID available as $id in this loop.

Step 3: Handle Reply Submission

In your PHP code where you handle form submissions, add logic to handle replies:

if(isset($_POST['reply_submit'])) {
    $parent_id = $_POST['parent_id'];
    $reply_comment = $_POST['reply_comment'];

    $sql = "INSERT INTO comments (comment, user, date, parent_id) VALUES ('$reply_comment', '$username', NOW(), '$parent_id')";
    $query = mysqli_query($conn, $sql) or die('Could not connect to the database');
}

Step 4: Display Replies

You’ll need to update the way you fetch and display comments to handle threading. This can be a bit more complex, depending on how you want to structure replies. A simple way is to create a recursive function that fetches and displays all child comments for a given parent comment.

Good luck

Sponsor our Newsletter | Privacy Policy | Terms of Service