Home > Article > Backend Development > PHP implements the user attention function in the knowledge question and answer website.
PHP implements the user attention function in the knowledge question and answer website
With the rapid development of the Internet, the knowledge question and answer website has gradually become an important way for people to obtain information and solve problems. In order to better meet user needs, an important function is user following, that is, users can follow issues, topics or other users of interest to obtain relevant updates and reminders. This article will introduce how to use PHP to implement the user attention function in a knowledge question and answer website, and attach a code sample.
You can use the following SQL statement to create this table:
CREATE TABLE `follows` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `follower_id` int(11) unsigned NOT NULL, `following_id` int(11) unsigned NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `follower_following_unique` (`follower_id`,`following_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<?php // 获取当前用户的 ID,可以从登录信息中获取。 $currentUser = 1; // 获取被关注用户的 ID,可以从用户界面中获取。 $followingUser = 2; // 连接数据库 $pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password'); // 检查是否已经关注 $sql = "SELECT COUNT(*) FROM follows WHERE follower_id = :follower AND following_id = :following"; $query = $pdo->prepare($sql); $query->bindParam(':follower', $currentUser); $query->bindParam(':following', $followingUser); $query->execute(); $alreadyFollowing = $query->fetchColumn() > 0; if ($alreadyFollowing) { // 如果已经关注,则取消关注 $sql = "DELETE FROM follows WHERE follower_id = :follower AND following_id = :following"; } else { // 如果未关注,则添加关注 $sql = "INSERT INTO follows (follower_id, following_id) VALUES (:follower, :following)"; } $query = $pdo->prepare($sql); $query->bindParam(':follower', $currentUser); $query->bindParam(':following', $followingUser); $query->execute(); // 关闭数据库连接 $pdo = null; ?>
The logic of the above code is to first query the database to determine whether the current user has followed the followed user. If you are already paying attention, execute the SQL to delete the attention; if not, execute the SQL to add the attention. Through this logic, users can follow and unfollow functions by clicking the button.
It should be noted that the sample code is for reference only. The actual situation needs to be adjusted and improved according to specific project needs, such as adding permission control, error handling, etc.
Summary
This article introduces how to use PHP to implement the user attention function in a knowledge question and answer website. By designing database tables and writing corresponding PHP scripts, we can easily implement the functions of users following and unfollowing. This feature is very useful for users and helps them get interesting content and reminders. I hope this article can help you understand and implement the user attention function.
The above is the detailed content of PHP implements the user attention function in the knowledge question and answer website.. For more information, please follow other related articles on the PHP Chinese website!