3
I have two tables, one of posts (posts) and another of friends(Friends). In my script there is an area of feed where I would like to present the latest posts from the logged user’s friends.
But instead of bringing the latest posts in order, the result orders the list of friends and the limit of posts imposed in the query of the table "posts" bringing this amount to each user (friend).
Explaining: The result brings for example the 3 last posts of each friend, but if I have in the system 4 friends it presents the three last of the first (friend), after the second and so on instead of three of each but ordered by the last.
I saw examples on the internet but could not understand the logic of how to make the filtering by the table "Friends" does not interfere in the sorting of the table display "posts".
Could someone help me understand that? Thank you.
<?php
header ('Content-type: text/html; charset=UTF-8');
require "connect_db.php";
$userID = $_SESSION['user_released'];
$BuscaFriends = $pdo->query("SELECT * FROM friends WHERE userID LIKE '%$userID%'");
$BuscaFriends->execute();
if($BuscaFriends->rowCount() == ""){
?>
<!-- Trecho HTML Caso não tenha amigos na tabela "friends" !!! -->
<?php
}else{
while($fetch = $BuscaFriends->fetch(PDO::FETCH_ASSOC)){
$idFriend = $fetch['friendID'];
$statusFriend = $fetch['status'];
$BuscaPost = $pdo->query("SELECT * FROM posts WHERE autor LIKE '%$idFriend%' ORDER By id DESC LIMIT 3");
$BuscaPost->execute();
while($fetch = $BuscaPost->fetch(PDO::FETCH_ASSOC)){
$autor = $fetch['autor'];
$data = $fetch['data'];
$ava = $fetch['autor_avatar'];
$msgID = $fetch['id'];
$msg = $fetch['content'];
$Verific = $pdo->prepare("SELECT * FROM users WHERE `user` = :user");
$Verific->bindParam(':user', $autor, PDO::PARAM_STR);
$Verific->execute();
while($fatch = $Verific->fetch(PDO::FETCH_ASSOC)){
$user = $fatch['user'];
$autorAvatar = $fatch['avatar'];
$autorCover = $fatch['cover'];
}
?>
<!-- Trecho HTML que retorna resultados !!! -->
<?php
}//Fecha "while" BuscaPost
}//Fecha "while" BuscaFriends
}//Fecho "else" amigos
?>
I saw your example but I don’t know how to mount your example returns syntax error...I’m trying to standardize my connections with Pdo as my tables friendID is not a bad "id" yes the user name as well as userid...but each entry in the table gets an id (id int 11)
– Lauro Moraes
@Lauromoraes Just adjust the syntax to match the structure of your database. The logic is exactly the same.
– André Ribeiro
In fact I did not know JOIN followed his example and I was able to fit now I could understand this correlation
– Lauro Moraes