With the development of the internet, more and more websites are using content management systems (CMS) to manage and publish website content. Among these websites, comment functionality is a vital interaction channel with users. Allowing users to reply to others' comments is an essential feature to enhance user experience.
This tutorial will explain how to implement the comment reply functionality in a CMS system using PHP and store comment data in a MySQL database. We will walk through the PHP code needed to add, display, and reply to comments.
First, we need to create a table in the database to store comment data. Below is an example of the table structure:
CREATE TABLE comments ( id INT AUTO_INCREMENT PRIMARY KEY, parent_id INT, content TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP );
In this table structure, id is the unique identifier for each comment, parent_id represents the parent comment (if it's a top-level comment, it will be NULL), content stores the comment text, and created_at stores the creation time of the comment.
Next, we need to write PHP code to fetch and display the comment data from the database. Below is an example of fetching the comments:
<?php $conn = mysqli_connect("localhost", "username", "password", "database"); // Fetch top-level comments $query = "SELECT * FROM comments WHERE parent_id IS NULL"; $result = mysqli_query($conn, $query); // Iterate through comments and fetch child comments while ($row = mysqli_fetch_assoc($result)) { echo "<div class='comment'>"; echo "<p>" . $row['content'] . "</p>"; // Fetch child comments $subQuery = "SELECT * FROM comments WHERE parent_id = " . $row['id']; $subResult = mysqli_query($conn, $subQuery); // Display child comments while ($subRow = mysqli_fetch_assoc($subResult)) { echo "<div class='sub-comment'>"; echo "<p>" . $subRow['content'] . "</p>"; echo "</div>"; } // Display reply form echo "<form class='reply-form' action='reply.php' method='POST'>"; echo "<input type='hidden' name='parent_id' value='" . $row['id'] . "'>"; echo "<textarea name='content'></textarea>"; echo "<input type='submit' value='Reply'>"; echo "</form>"; echo "</div>"; } mysqli_close($conn); ?>
In this example, we connect to the database and fetch the top-level comments. We then loop through each comment and fetch its child comments. For each comment, we display its content, followed by its child comments, and then a reply form where users can submit their replies.
When users submit a reply, we need to save the reply content in the database. Here is an example of the PHP code for saving replies:
<?php $conn = mysqli_connect("localhost", "username", "password", "database"); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $parent_id = $_POST['parent_id']; $content = $_POST['content']; // Insert reply into the database $query = "INSERT INTO comments (parent_id, content) VALUES ('$parent_id', '$content')"; mysqli_query($conn, $query); } mysqli_close($conn); ?>
In the code above, we first check if the request method is POST. If so, we retrieve the parent comment's ID and the user's reply content, and insert the reply into the database.
This tutorial has explained how to implement the comment reply functionality in a CMS system using PHP. We used MySQL to store the comment data and PHP to handle the adding, displaying, and replying to comments. This functionality can significantly improve user interaction with your website. We hope this example code will help you implement your own comment system.