I am trying to build a system that retrieves posts from users that a logged-in user happens to "follow". The system works, however user posts do not get arranged the way I would like. That is, they are arrange in clusters/sets depending on the user id that has been queried.
For example, if user one has three posts, his three posts will be grouped together; if user two has five posts, his five posts will be grouped together, irrespective of what that post's id is relative to posts other users have made.
I would like posts not to cluster together, but to display in order (of id or date) irrespective of the user that has made the post.
I am not sure if I have explained the problem well enough, but here is my server-side code [below].
Any help is greatly appreciated. Thank you
PHP/MYSQL
public
function get_posts_of_followed() {
include "connect.php";
$userid = $this->getUserId();
// Posts from users the loggedin user follows
$postArray = array();
// Get the user_ids of the people the loggedin user follows
$followed = array();
$get_followed_query = "SELECT `followed_id` FROM `user_follows` WHERE `follower_id` = '$userid'";
$get_followed_result = mysqli_query($con, $get_followed_query);
while($row_followed = mysqli_fetch_assoc($get_followed_result)){
$ids = array("followed_ids" => $row_followed['followed_id']);
array_push($followed, $ids);
}
foreach($followed as $theirid){
// Get post_data from users the loggedin user follows
$query_posts = "SELECT `id`,`post_title`,`user_id`, `post`, `date`, `category`
FROM `user_posts`
WHERE `user_id`= '". $theirid['followed_ids']."' ORDER BY `id` DESC";
$post_result = mysqli_query($con, $query_posts);
while($postData = mysqli_fetch_assoc($post_result)){
// Get profile_data from the users the loggedin user follows
$query_userData = "SELECT `id`,`firstname`, `lastname`, `profilepic` FROM `users` WHERE `id` = '". $postData['user_id'] ."'";
$userData_result = mysqli_query($con, $query_userData);
while($userData = mysqli_fetch_assoc($userData_result)){
$data = array("firstname" => $userData['firstname'], "lastname" => $userData['lastname'], "profilepic" => $userData['profilepic'], "post_title" => $postData['post_title'], "user_id" => $userid, "date" => $postData['date'], "post_id" => $postData['id'], "the_post" => $postData['post'],"category" => $postData['category']);
array_push($postArray, $data);
}
}
}
return $postArray;
}
}