PHP realizes the statistical functions of user activity and contribution in knowledge question and answer websites
With the rise of knowledge question and answer websites, more and more people tend to obtain and share various information on such platforms. Knowledge. In order to increase user activity and encourage users to make more contributions, we can evaluate their activity and contribution by counting their activity data on the website. This article explains how to implement this functionality using PHP.
First of all, we need to understand the concepts of user activity and contribution. User activity refers to the frequency and number of users posting questions, answering questions, liking, commenting and other activities on the website within a certain period of time. Contribution refers to the specific contributions made by users on the website, such as answering questions, solving problems, posting high-quality questions, etc.
In our implementation, we define user activity as the frequency of activities in the past month, including posting questions, answering questions, likes, comments, etc. Contribution is the total number of questions posted and answered by users on the website.
First of all, we need to have a user table to store the user's basic information, such as user ID, user name, etc. We can create a database table called users and create the corresponding fields in it.
CREATE TABLE users
(
id
int(11) NOT NULL AUTO_INCREMENT,
username
varchar(255) NOT NULL ,
email
varchar(255) NOT NULL,
created_at
datetime NOT NULL,
PRIMARY KEY (id
)
) ;
Next, we need an activity table to record the user's activity information, including user ID, activity type, activity time, etc. We can create a database table called activities and create the corresponding fields in it.
CREATE TABLE activities
(
id
int(11) NOT NULL AUTO_INCREMENT,
user_id
int(11) NOT NULL ,
activity_type
varchar(255) NOT NULL,
created_at
datetime NOT NULL,
PRIMARY KEY (id
)
) ;
When the user performs an activity, we need to insert a record into the activity table accordingly. For example, when a user posts a question, we can execute the following code:
$user_id = $_SESSION['user_id']; // Get the current user ID
$activity_type = 'question'; // Activity Type is issue release
$created_at = date('Y-m-d H:i:s'); // Current time
$sql = "INSERT INTO activities (user_id, activity_type, created_at)
VALUES ('$user_id', '$activity_type', '$created_at')";
$query = mysqli_query($conn, $sql); //Perform insert record operation
Similarly, when the user answers a question, likes or comments, we also need to execute similar code.
Next, we need to write a function to count the user's activity and contribution. We can create a function named get_user_activity() to query the user's activity records in the last month.
function get_user_activity($user_id) {
$past_month = date('Y-m-d', strtotime('-1 month')); // Get the date one month ago
$sql = " SELECT count(*) as activity_count
FROM activities WHERE user_id = $user_id AND created_at >= '$past_month'";
$query = mysqli_query($conn, $sql);
$result = mysqli_fetch_assoc($query);
return $result['activity_count '];
}
In addition, we can also create a function called get_user_contribution() to query the total number of questions posted and answered by users on the website.
function get_user_contribution($user_id) {
$sql = "SELECT count(*) as contribution_count
FROM activities WHERE user_id = $user_id AND (activity_type = 'question' OR activity_type = 'answer')";
$query = mysqli_query($conn, $sql);
$ result = mysqli_fetch_assoc($query);
return $result['contribution_count'];
}
Through the above code, we can call these two functions in the page to obtain users Activity and contribution.
$user_id = $_SESSION['user_id']; // Get the current user ID
// Get the user activity and display it on the page
$user_activity = get_user_activity($user_id);
echo "User activity:" . $user_activity;
// Get user contribution and display it on the page
$user_contribution = get_user_contribution($user_id );
echo "User contribution:" . $user_contribution;
Through the above steps, we have completed the PHP code to implement user activity and contribution statistics functions in the knowledge question and answer website. We can Expand as needed, and adjust the time range and activity type according to the actual situation.
To sum up, by counting users’ activity data on the knowledge question and answer website, we can evaluate the user’s activity and contribution, so as to Encourage users to participate more actively in Q&A activities on the website. This is of great significance to improving website activity and user experience.
The above is the detailed content of PHP implements user activity and contribution statistics functions in the knowledge question and answer website.. For more information, please follow other related articles on the PHP Chinese website!