如何仅显示好友列表中的帖子.(正确)


How to show posts from friends list only. (correctly)

My friends数组只返回一个数字,而不是所有数字。

($myfriends = 3)

应该是…

($myfriends = 3 5 7 8 9 12).

如果我把它转到while循环。。。整件事都有效,但不是像我想的那样按日期顺序显示帖子。示例:

(post_id 3 oct30 "post goes here")
(post_id 5 oct29 "post goes here")
(post_id 7 oct28 "post goes here")

相反,它显示了按日期排序的1个人id的所有帖子。然后在那个人发了所有的帖子之后。它将最终显示下一个人及其所有帖子。:

(post_id 3 oct30 "post goes here")
(post_id 3 oct29 "post goes here")
(post_id 3 oct28 "post goes here")
(post_id 5 oct30 "post goes here")
(post_id 5 oct29 "post goes here")
(post_id 5 oct28 "post goes here")

等等…任何新的帖子都会将用户的旧帖子分组,而不是转到订阅源的顶部。

我希望这是有道理的。。。很抱歉解释不周,代码很草率


TABLES馈送&下面的朋友两者都将自动递增主"id"作为第一个字段。。。然后

FEED TABLE
feed (post_id, posted_by, content, date_posted, date_str, pic) VALUES ('$post_id', '$posted_by', '$content', now(), '$date', '$profile_pic')
FRIENDS TABLE
friends (user_id, friend_id) VALUES ('$myId', '$friendId')

<div class="post container">
<?php
$addsql="SELECT * FROM friends WHERE user_id = '$session_id' ORDER by id DESC";
$addquery=mysqli_query($conn, $addsql);
while ($rowa = mysqli_fetch_array($addquery)) {
$myFriends = $rowa['friend_id'];      
?>
<?php
$feedsql = "SELECT * FROM feed WHERE post_id = '$myFriends' ORDER by date_posted DESC";
$result = mysqli_query($conn, $feedsql);
while($row = mysqli_fetch_array($result)) {
$posted_by = $row['posted_by'];
$content = $row['content'];
$time = $row['date_posted'];
$date = $row['date_str'];
$pic = $row['pic'];
$post_id = $row['post_id'];
$upperUser = ucfirst($username);
?>
<?php 
$imgsql = "SELECT * FROM users WHERE username = '$posted_by' ";
$q = mysqli_query($conn, $imgsql);
while($rows = mysqli_fetch_array($q)) {
$image = $rows['image'];
$mem_id = $rows['id'];
if ($posted_by == $upperUser) {
echo " <a href='profile.php?id=$mem_id'><img  src='users/$upperUser/".$image."' alt='MEMBER Pic' /></a>"; 
} else if($posted_by !== $upperUser) {
echo " <a href='profile.php?id=$mem_id'><img  src='users/$posted_by/".$image."' alt='Profile Pic' /></a>"; 
}
} 
?> 
 <div class="each_post">
<h4><a href='profile.php?id=<?php echo $mem_id; ?>'><?php echo ucfirst($posted_by);?></a>  <small class="post_date"><?php echo $date;?></small></h4>
<p class=""><?php echo $content;?></p>
</div>
<?php
}}
?>
</div>

问题本身是由while循环中重写$myFriends变量引起的:

while ($rowa = mysqli_fetch_array($addquery)) {
    $myFriends = $rowa['friend_id'];      
?>

您可以将$myFriends定义为一个数组,并将friend_ids添加到该数组中,但是您不应该这样做,因为这是低效的。

您可以使用SQL中的join关键字连接多个表。在这种特殊情况下,您将需要一个INNER JOIN(有几种类型的联接,但了解它们是您的任务)。

$feedsql = "SELECT * FROM feed INNER JOIN friends ON feed.posted_by=friends.friend_id WHERE friends.user_id = '$session_id' ORDER by date_posted DESC";

由于您没有公开您的表结构,我假设提要表中的posted_by字段将决定谁发送了帖子,所以我可以在friend_id字段中加入这个字段。如果不是这样,那么您需要确定这两个表可以在哪些字段上连接

你甚至可以加入上面两个表中的用户表来获得朋友的详细信息。