Use PHP to develop the question browsing history and recording functions in the knowledge Q&A website.

王林
Release: 2023-07-02 08:54:01
Original
1354 people have browsed it

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:

  • After users log in, they can view a list of questions they have recently viewed, making it easier for them to review and continue reading.
  • The browsing history will save the user’s 10 most recent questions. When this number is exceeded, the oldest record will be deleted.

Implementation process:

  1. Database design
    First, we need to create a data table for storing browsing history. We can do this using a MySQL database. Create a data table named "history", containing the following fields:
  2. id: record unique identifier, using auto-incrementing primary key.
  3. user_id: User ID, indicating which user the record belongs to.
  4. question_id: Question ID, indicating the browsing question.
  5. timestamp: The timestamp of the record, used for sorting and limiting the maximum number of records.
CREATE TABLE history ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT, question_id INT, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
Copy after login
  1. PHP code implementation
    Next, we will implement the browsing history and recording functions through PHP code.
// 设置数据库连接 $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 "
"; echo $row_question['content']; echo "

"; } } } } else { echo "还没有浏览历史记录"; } // 关闭数据库连接 $conn->close();
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!