Good day
am trying to display users’s posts for their friends only, this functionality works but it doesn’t work properly, here is my code
Here i get friends for a specific user
function getfriends(){
$db = new Database(true);
$my_id = $_SESSION['user_id'];
$return_data = [];
$db->query('SELECT * FROM friends WHERE (user_one = :my_id OR user_two = :my_id2)');
$db->bind(':my_id', $my_id);
$db->bind(':my_id2', $my_id);
$all_users = $db->resultset();
foreach($all_users as $row){
if($row->user_one == $my_id){
$db->query('SELECT * FROM users WHERE user_id = :user_id');
$db->bind(':user_id', $row->user_two);
$results = $db->resultset();
array_push($return_data, $results);
}else{
$db->query("SELECT * FROM users WHERE user_id = :user_id1");
$db->bind(':user_id1', $row->user_one);
$results = $db->resultset();
array_push($return_data, $results);
}
}
//die(print_r($return_data,true));
return $return_data;
}
Here if a user is in the friendlist i show their posts including the logged in user
function isMyFriend($userToCheck){
$db = new Database(true);
$user_id = $_SESSION['user_id'];
$db->query('SELECT * FROM users WHERE user_id = :user_id');
$db->bind(':user_id', $user_id);
$row = $db->single();
@$userna = $row->username;
foreach (getfriends() as $key) {
foreach ($key as $value) {
$friendArray = $value->username;
}
}
if ((strstr($friendArray,$userToCheck) || $userToCheck == $userna)) {
return true;
}else {
return false;
}
}
The current user has 3 friends but this shows current user posts and one friends posts as for other 2 it doesn’t show their posts. I tried putting the if statement inside the for-each loop but it still shows one friend post, is there any way i can iterate through the usernames to make their posts show?
I use this function like this:
if(isMyFriend($post->user_id)){
//here i display posts
}
Please help
Thanks in advance