Home  >  Article  >  Backend Development  >  How to use php to implement the reply function of message board

How to use php to implement the reply function of message board

PHPz
PHPzOriginal
2023-04-26 18:00:561383browse

With the rapid development of the Internet, message boards have become an essential part of many websites. Among many message boards, the reply function is particularly important because it allows message writers to interact with website administrators, making communication smoother. This article will introduce how to use php to implement the reply function of the message board.

1. Database design

Before implementing the message board reply function, you first need to design a data table to store the message and reply content. This article uses the MySQL database. The following is the design of the message table and reply table:

Message table (message)

##idint(11)Message IDusernamevarchar(20)Name of the commentercontentvarchar( 255)Message contentcreated_atdatetimeMessage time updated_atdatetimeModification time
Column name Data type Explanation
Reply table (reply)

Column nameData typeExplanation##idmessage_idusernamecontentcreated_atupdated_atThe association between the message table and the reply table is achieved through message_id, that is, a certain reply corresponds to a certain message. In addition, in order to facilitate management, two fields, creation time and modification time, are added to each message and reply, which will be used in the subsequent development process.
int(11) Reply ID
int(11) Message ID
varchar(20) Replyer name
varchar(255) Reply content
datetime Reply time
datetime Modification time

2. Page layout

After the database design is completed, the next step is page layout. For message boards, it is important that the interface is simple, beautiful and easy to use. This article uses the Bootstrap framework to design the message board page. The following is the code of the message board page:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container">
        <h2>留言板</h2>
        <form class="form-horizontal" method="post" action="post_message.php">
            <div class="form-group">
                <label for="username" class="col-sm-2 control-label">姓名:</label>
                <div class="col-sm-10">
                    <input type="text" name="username" class="form-control" required>
                </div>
            </div>
            <div class="form-group">
                <label for="content" class="col-sm-2 control-label">留言内容:</label>
                <div class="col-sm-10">
                    <textarea name="content" class="form-control" rows="5" required></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-primary">提交</button>
                </div>
            </div>
        </form>
        <?php
            // 查询留言列表
            $sql = "SELECT * FROM message ORDER BY created_at DESC";
            $result = mysqli_query($conn, $sql);
            while ($row = mysqli_fetch_assoc($result)) {
        ?>
        <div class="panel panel-default">
            <div class="panel-heading">
                <?php echo $row[&#39;username&#39;]; ?>(<?php echo $row[&#39;created_at&#39;]; ?>)
            </div>
            <div class="panel-body">
                <?php echo $row[&#39;content&#39;]; ?>
            </div>
        </div>
        <?php
                // 查询回复列表
                $sql = "SELECT * FROM reply WHERE message_id = {$row[&#39;id&#39;]} ORDER BY created_at ASC";
                $result2 = mysqli_query($conn, $sql);
                while ($row2 = mysqli_fetch_assoc($result2)) {
        ?>
        <div class="panel panel-info">
            <div class="panel-heading">
                <?php echo $row2[&#39;username&#39;]; ?>(<?php echo $row2[&#39;created_at&#39;]; ?>)
            </div>
            <div class="panel-body">
                <?php echo $row2[&#39;content&#39;]; ?>
            </div>
        </div>
        <?php
                }
        ?>
        <form class="form-horizontal" method="post" action="post_reply.php">
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <input type="hidden" name="message_id" value="<?php echo $row[&#39;id&#39;]; ?>">
                    <input type="text" name="username" class="form-control" placeholder="回复者姓名" required>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <textarea name="content" class="form-control" rows="3" placeholder="回复内容" required></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">回复</button>
                </div>
            </div>
        </form>
        <?php
            }
        ?>
    </div>
</body>
</html>

In the above code, the message board page is divided into two parts, the upper part is the message box, and the lower part is the message list. and reply form. Both the message list and the reply form are dynamically generated. The reply form of each message has a hidden message_id field to facilitate server-side processing.

3. Back-end implementation

After the page layout is completed, the next step is back-end implementation. In this article, the object-oriented PHP programming method is adopted to facilitate code expansion and maintenance. The following is the PHP code:

<?php
// 建立数据库连接
$conn = new mysqli("localhost", "root", "password", "test");
if ($conn->connect_error) {
    die("数据库连接失败:" . $conn->connect_error);
}

// 定义Message类
class Message {
    public $id;
    public $username;
    public $content;
    public $created_at;
    public $updated_at;
    
    // 构造函数
    function __construct($id, $username, $content, $created_at, $updated_at) {
        $this->id = $id;
        $this->username = $username;
        $this->content = $content;
        $this->created_at = $created_at;
        $this->updated_at = $updated_at;
    }
    
    // 静态方法:获取留言列表
    static function get_list($conn) {
        $messages = array();
        $sql = "SELECT * FROM message ORDER BY created_at DESC";
        $result = mysqli_query($conn, $sql);
        while ($row = mysqli_fetch_assoc($result)) {
            $message = new Message($row['id'], $row['username'], $row['content'], $row['created_at'], $row['updated_at']);
            array_push($messages, $message);
        }
        return $messages;
    }
}

// 定义Reply类
class Reply {
    public $id;
    public $message_id;
    public $username;
    public $content;
    public $created_at;
    public $updated_at;
    
    // 构造函数
    function __construct($id, $message_id, $username, $content, $created_at, $updated_at) {
        $this->id = $id;
        $this->message_id = $message_id;
        $this->username = $username;
        $this->content = $content;
        $this->created_at = $created_at;
        $this->updated_at = $updated_at;
    }
    
    // 静态方法:根据留言ID获取回复列表
    static function get_list_by_message_id($conn, $message_id) {
        $replies = array();
        $sql = "SELECT * FROM reply WHERE message_id = {$message_id} ORDER BY created_at ASC";
        $result = mysqli_query($conn, $sql);
        while ($row = mysqli_fetch_assoc($result)) {
            $reply = new Reply($row['id'], $row['message_id'], $row['username'], $row['content'], $row['created_at'], $row['updated_at']);
            array_push($replies, $reply);
        }
        return $replies;
    }
}

// 处理留言提交
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['username']) && !empty($_POST['content'])) {
    $sql = "INSERT INTO message (username, content) VALUES ('{$_POST['username']}', '{$_POST['content']}')";
    mysqli_query($conn, $sql);
}

// 处理回复提交
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['message_id']) && !empty($_POST['username']) && !empty($_POST['content'])) {
    $sql = "INSERT INTO reply (message_id, username, content) VALUES ({$_POST['message_id']}, '{$_POST['username']}', '{$_POST['content']}')";
    mysqli_query($conn, $sql);
}

// 获取留言列表
$messages = Message::get_list($conn);
?>

In the above code, the Message class and Reply class are defined, corresponding to messages and replies. In the Message class, the static method get_list is defined to obtain the message list; in the Reply class, the static method get_list_by_message_id is defined to obtain the reply list. At the same time, PHP's built-in $_POST array is used to process form submission data, and message and reply data are inserted into the corresponding data table.

At this point, the entire process of implementing the message board reply function in PHP has been completed. Through the design and implementation of the above code, we can easily implement the message board reply function, which improves the user experience and facilitates website management.

The above is the detailed content of How to use php to implement the reply function of message board. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn