With the widespread popularity of social networks, mutual following has become an essential way to connect users. By following each other, users can receive real-time updates on their contacts' activities and interact more effectively. Before developing the mutual follow feature, it is important to clarify the core requirements:
To support the mutual follow feature, the database structure needs to be designed accordingly:
$follower_id = $_GET['follower_id']; // Get follower ID
$followed_id = $_GET['followed_id']; // Get followed user ID
$follow_time = time(); // Get current timestamp
$sql = "INSERT INTO following (follower_id, followed_id, follow_time) VALUES ($follower_id, $followed_id, $follow_time)";
$result = mysqli_query($conn, $sql);
$user_id = $_GET['user_id']; // Get user ID
// Query follow list
$sql1 = "SELECT * FROM following WHERE follower_id = $user_id";
$result1 = mysqli_query($conn, $sql1);
// Query fan list
$sql2 = "SELECT * FROM following WHERE followed_id = $user_id";
$result2 = mysqli_query($conn, $sql2);
// Output follow list
while ($row1 = mysqli_fetch_assoc($result1)) {
$followed_id = $row1['followed_id'];
$followed_user = mysqli_query($conn, "SELECT * FROM user WHERE user_id = $followed_id");
$row = mysqli_fetch_assoc($followed_user);
echo $row['username']; // Output followed user's name
}
// Output fan list
while ($row2 = mysqli_fetch_assoc($result2)) {
$follower_id = $row2['follower_id'];
$follower_user = mysqli_query($conn, "SELECT * FROM user WHERE user_id = $follower_id");
$row = mysqli_fetch_assoc($follower_user);
echo $row['username']; // Output fan's name
}
$user_id = $_GET['user_id']; // Get user ID
// Query follow list
$sql = "SELECT * FROM following WHERE follower_id = $user_id";
$result = mysqli_query($conn, $sql);
// Output dynamic updates
while ($row = mysqli_fetch_assoc($result)) {
$followed_id = $row['followed_id'];
$dynamic = mysqli_query($conn, "SELECT * FROM dynamic WHERE user_id = $followed_id ORDER BY dynamic_time DESC LIMIT 10");
while ($row = mysqli_fetch_assoc($dynamic)) {
echo $row['content']; // Output dynamic content
}
}
The core of implementing a mutual follow feature lies in CRUD operations on the database. By designing a proper database schema and writing corresponding PHP code, user follow relationships and dynamic content display can be efficiently managed. Developers can further enhance this functionality by adding notifications, grouping follows, and more. The sample code provided here offers a clear approach to quickly integrating this feature into your projects.