PHP implements the question browsing history and recording functions in the knowledge question and answer website.

WBOY
Release: 2023-07-02 10:46:02
Original
1003 people have browsed it

PHP implements the question browsing history and recording functions in the Q&A website

With the rapid development of the Internet, the Q&A website is becoming more and more popular. In such a website, users can ask questions and get answers from other users. In order to improve the user experience, sometimes we need to implement question browsing history and recording functions so that users can easily view previously browsed questions.

In this article, we will use PHP to achieve this functionality. We will first create a database table to store the user's browsing history, and then write the corresponding PHP code to process and display these records.

Create database table

First, we need to create a database table named question_history to save the user's browsing history. The table contains the following fields:

  • id: primary key, unique identifier of the record
  • user_id: unique identifier of the user, used to associate users and browsing history
  • question_id: The unique identifier of the browsed question
  • timestamp: The timestamp of the record, used to sort the records by time

The following is the SQL statement to create the question_history table:

CREATE TABLE `question_history` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `question_id` int(11) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

PHP code implementation

Next, we need to write some PHP code to process and display the user's browsing history. First, we need to write a function to insert the user's browsing history into a database table. This function accepts two parameters: the user's unique identifier and the unique identifier of the question being viewed.

function insertQuestionHistory($user_id, $question_id) {
  // 创建数据库连接
  $conn = new mysqli("localhost", "username", "password", "database");

  // 检查连接是否成功
  if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
  }

  // 插入记录到 question_history 表
  $sql = "INSERT INTO question_history (user_id, question_id) VALUES ('$user_id', '$question_id')";
  if ($conn->query($sql) === TRUE) {
    echo "浏览历史记录插入成功";
  } else {
    echo "插入错误:" . $conn->error;
  }

  // 关闭数据库连接
  $conn->close();
}
Copy after login

Then, we can write a function to get the user's browsing history from the database. This function also receives the user's unique identifier as a parameter and returns an array containing the user's browsing history.

function getQuestionHistory($user_id) {
  // 创建数据库连接
  $conn = new mysqli("localhost", "username", "password", "database");

  // 检查连接是否成功
  if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
  }

  // 查询 question_history 表中的记录
  $sql = "SELECT * FROM question_history WHERE user_id = '$user_id' ORDER BY timestamp DESC";
  $result = $conn->query($sql);

  // 将查询结果转换为数组
  $history = array();
  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      $history[] = $row;
    }
  }

  // 关闭数据库连接
  $conn->close();

  // 返回浏览历史记录数组
  return $history;
}
Copy after login

Finally, we can use the above function to display the user’s browsing history. We can iterate through the browsing history array and print out each record.

$user_id = 1; // 假设用户的唯一标识符为 1

// 获取用户的浏览历史记录
$history = getQuestionHistory($user_id);

// 打印浏览历史记录
foreach ($history as $row) {
  echo "问题ID:" . $row['question_id'] . ",浏览时间:" . $row['timestamp'] . "<br>";
}
Copy after login

Summary

Through the above PHP code example, we successfully implemented the question browsing history and recording functions in the knowledge Q&A website. Users can record and obtain their browsing history by inserting and querying database tables, and display it to the user through PHP code. Such a function can improve the user experience and make it easier for users to view previously browsed questions.

The above is the detailed content of PHP implements the question browsing history and recording functions in the knowledge question and answer 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template