I have built a Twitter like following system using PHP and Mysql and I would like to list the followers a user has in order in which they followed the user. There are two relational tables that pertain to this action: USERS and FOLLOW.
user_id
the one that is being followed.
follow_id
the one that is following.
created
date in which the following happened.
FOLLOW
--------------------------------------------------
|user_id |follow_id | created |
--------------------------------------------------
| 23 | 5 |2016-08-09 |
--------------------------------------------------
USERS
--------------------------------------------------
|user_id | name |
-------------------------------------------------
| 5 |John Doe|
--------------------------------------------------
This the php Mysql function to get the followers for a certain user.
<?php
public static function find_followers($user_id){
global $database;
$followers_ids = array();
$sql = "SELECT user_id
FROM follow
WHERE followed_id = '{$user_id}'
ORDER BY created";
$result = $database->query($sql);
while($follower = $database->fetch_array($result)){
array_push($followers_ids, $follower['user_id']);
}
if(count($followers_ids)){
$id_strings = join(',', $followers_ids);
$sql = "SELECT * FROM users WHERE id IN ($id_strings)";
$followers = self::find_by_sql($sql);
return $followers;
}
}
?>
Code to dislay followers
<?php
$followers = find_followers($id);
foreach($followers as $follower){
echo $follower->full_name() . "followed you.<br/>";
?>
Currently the list of followers is being ordered by the creation on which users were created, and I would like to display them based upon when the order in which they followed the user. How can this be done?
Thanks in advance!