Use PHP to develop user search history and recommendation functions in knowledge question and answer websites
In many knowledge question and answer websites, user search history and recommendation functions are one of the important functions. They can provide a personalized search experience and make it easier for users to find content that interests them. This article explores how to develop these features using PHP.
First, we need to create a database to store the user's search history and recommended content. We can use MySQL or other relational databases to achieve this. The following is a simple example of the search history table structure:
CREATE TABLE `search_history` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `keyword` varchar(255) NOT NULL, `search_time` datetime NOT NULL, PRIMARY KEY (`id`), KEY `user_id` (`user_id`), KEY `keyword` (`keyword`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Next, we will use PHP to implement the recording function of user search history. Suppose we already have a user logged into the system, and the user performs a search operation after logging in. The following is a sample code:
// 假设用户已登录,获取用户ID $user_id = $_SESSION['user_id']; // 获取用户搜索关键词 $keyword = $_POST['keyword']; $search_time = date('Y-m-d H:i:s'); // 将搜索历史插入数据库 $query = "INSERT INTO search_history (user_id, keyword, search_time) VALUES ('$user_id', '$keyword', '$search_time')"; mysqli_query($connection, $query);
The above code inserts the user's search keywords, user ID and search time into thesearch_history
table of the database.
Next, we will implement the recommendation function. Recommended content can be generated based on the user's search history and the search behavior of other users. The following is a sample code:
// 获取当前用户的搜索历史 $query = "SELECT keyword FROM search_history WHERE user_id = '$user_id'"; $result = mysqli_query($connection, $query); // 将搜索历史拼接为字符串,用于生成推荐内容 $search_history = ''; while ($row = mysqli_fetch_assoc($result)) { $search_history .= $row['keyword'] . ' '; } // 查询其他用户可能感兴趣的内容 $query = "SELECT keyword, COUNT(*) AS count FROM search_history WHERE user_id != '$user_id' AND keyword NOT IN ($search_history) GROUP BY keyword ORDER BY count DESC LIMIT 5"; $result = mysqli_query($connection, $query); // 输出推荐内容 while ($row = mysqli_fetch_assoc($result)) { echo $row['keyword'] . '
'; }
The above code first obtains the current user's search history and concatenates it into a string. It then queries the database to find content that other users may be interested in, sorting it in descending order by search count, returning only the top 5 keywords.
Through the above code examples, we can use PHP to develop user search history and recommendation functions in the knowledge question and answer website. In this way, we can provide users with a personalized search experience and make it easier for them to find content of interest. Of course, this is just a simple example and you can modify and extend it according to your actual needs.
The above is the detailed content of Developed user search history and recommendation functionality in a trivia website using PHP.. For more information, please follow other related articles on the PHP Chinese website!