Home > Article > Backend Development > Use PHP to develop the question browsing history and recording functions in the knowledge Q&A website.
Use PHP to develop the question browsing history and recording functions in the Q&A website
Introduction:
The Q&A website is one of the most popular types of websites on the Internet today. In order to improve user experience, we can add problem browsing history and recording functions to this kind of website. This article will describe how to use PHP to develop this feature to help users more easily view the questions they have browsed.
Functional requirements:
Implementation process:
CREATE TABLE history ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT, question_id INT, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
// 设置数据库连接 $servername = "localhost"; $username = "your_username"; $password = "your_password"; $dbname = "your_database"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 获取当前登录用户的 ID $user_id = $_SESSION['user_id']; // 获取用户最近浏览的问题记录 $query = "SELECT * FROM history WHERE user_id = $user_id ORDER BY timestamp DESC LIMIT 10"; $result = $conn->query($query); // 显示浏览历史记录 if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $question_id = $row['question_id']; // 根据问题 ID 查询问题详情并显示 $query_question = "SELECT * FROM questions WHERE id = $question_id"; $result_question = $conn->query($query_question); if ($result_question->num_rows > 0) { while($row_question = $result_question->fetch_assoc()) { echo $row_question['title']; echo "<br>"; echo $row_question['content']; echo "<br><br>"; } } } } else { echo "还没有浏览历史记录"; } // 关闭数据库连接 $conn->close();
The above code first connects to the database, and then obtains the ID of the currently logged in user. Then the user's recent browsing history is queried from the database, and the problem details are queried and displayed based on the problem ID. Finally close the database connection.
Summary:
This article introduces through PHP code examples how to use PHP to develop question browsing history and recording functions in a knowledge question and answer website. Features like this improve the user experience and make it easier for users to view the questions they've browsed. Through database design and PHP code implementation, we can easily implement this functionality. I hope this article will be helpful to PHP developers and website developers with similar needs.
The above is the detailed content of Use PHP to develop the question browsing history and recording functions in the knowledge Q&A website.. For more information, please follow other related articles on the PHP Chinese website!