In modern social media applications, friend relationship management is a core feature that enables users to interact with each other. This functionality allows users to add friends, view friend lists, and send messages. In this article, we will demonstrate how to implement these features using PHP, with detailed code examples to guide you through the process.
First, we need to design a database to store user information and friend relationships. Typically, we will create two tables: `users` and `friends`.
The `users` table stores basic information about users, including user IDs, usernames, and passwords. Here's an example of how to create the `users` table:
CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
The `friends` table stores friend relationships, including the IDs of two users and the status of their relationship (e.g., pending, confirmed, etc.). Here's an example of how to create the `friends` table:
CREATE TABLE friends (
user_id1 INT(11) NOT NULL,
user_id2 INT(11) NOT NULL,
status INT(1) DEFAULT 0,
PRIMARY KEY (user_id1, user_id2),
FOREIGN KEY (user_id1) REFERENCES users(id),
FOREIGN KEY (user_id2) REFERENCES users(id)
);
To implement the "Add Friend" function, we first need to verify if the username entered by the user exists in the database. Assuming the username is entered and we query the database to confirm if the user exists:
$friendUsername = $_POST['friend_username'];
$friend = mysqli_fetch_assoc(mysqli_query("SELECT id FROM users WHERE username = '$friendUsername'"));
if ($friend) {
// Friend exists, can proceed to add friend
} else {
// Friend doesn't exist, please re-enter
}
If the friend exists, we then insert the friend relationship into the `friends` table:
$userID = $_SESSION['user_id'];
$friendID = $friend['id']; // Friend's ID retrieved from the previous query
mysqli_query("INSERT INTO friends (user_id1, user_id2, status) VALUES ('$userID', '$friendID', 0)");
if (mysqli_affected_rows() > 0) {
// Friend added successfully
} else {
// Failed to add friend, please try again later
}
Users can view their friend lists to manage and check their friends. Here's how we can retrieve the friend list of a user:
$userID = $_SESSION['user_id'];
$friends = mysqli_query("SELECT users.username
FROM friends
JOIN users ON friends.user_id2 = users.id
WHERE friends.user_id1 = '$userID' AND friends.status = 1");
while ($friend = mysqli_fetch_assoc($friends)) {
echo $friend['username'] . "<br>";
}
Users can send messages to their friends. The following code snippet demonstrates how to implement this feature:
$userID = $_SESSION['user_id'];
$friendUsername = $_POST['friend_username'];
$message = $_POST['message'];
$friend = mysqli_fetch_assoc(mysqli_query("SELECT id FROM users WHERE username = '$friendUsername'"));
if ($friend) {
$friendID = $friend['id'];
mysqli_query("INSERT INTO messages (sender_id, receiver_id, message)
VALUES ('$userID', '$friendID', '$message')");
if (mysqli_affected_rows() > 0) {
// Message sent successfully
} else {
// Failed to send message, please try again later
}
} else {
// Friend doesn't exist, please re-enter
}
Through the code examples provided in this article, you can see how to implement basic friend relationship management features using PHP. Of course, in real-world applications, there are many other considerations, such as additional functionality and security measures like input validation and SQL injection prevention. We hope this article helps developers better understand and implement the friend relationship management functionality in social media applications.