[英]Display following users posts
So I have a project that I'm working on where you can "follow" another user, and view their posts, along with other users. 因此,我有一个正在处理的项目,您可以在其中“跟随”另一个用户,并与其他用户一起查看他们的帖子。 Like Facebook.
像Facebook。 Something like this
像这样
Bob: I love SO
Alex: I love SO too
Ally: Me three!
So this is what I'm doing so far. 所以这就是我到目前为止所做的。 I'm selecting the people the signed in user is following, all of which are stored in a table called
followers
. 我选择登录用户所关注的人员,所有人员都存储在名为
followers
的表中。 Here's what it looks like 这是它的样子
+-----+-------------+-----------+
| id | follow_from | follow_to |
+-----+-------------+-----------+
| 319 | bob | alex |
| 320 | ally | alex |
| 320 | alex | ally |
+-----+-------------+-----------+
As you can see it displays the user and who they're following. 如您所见,它将显示用户及其关注的对象。 So this is how I retrieve a list of who you're following
所以这就是我如何检索您关注的人的列表
SELECT * FROM followers WHERE follow_from = :username
Now that will return 现在那会回来
Bob
Alex
Ally
Now onto the posts. 现在到帖子上。 I store the posts in another table called "posts".
我将帖子存储在另一个称为“帖子”的表中。 This is what that looks like
看起来像这样
+----+-----------+---------+-----------+--------------+------------+
| id | post_user | post_ip | post_date | post_content | post_likes |
+----+-----------+---------+-----------+--------------+------------+
| 1 | alex | ::1 | | I like cats | 0 |
+----+-----------+---------+-----------+--------------+------------+
So I'd assume that both Bob
and Ally
see alex's post. 所以我假定这两个
Bob
和Ally
看到Alex的帖子。 The issue I'm having is retrieving the posts and basically merging them together. 我遇到的问题是检索帖子并基本上将它们合并在一起。 Since my posts are in another table I couldn't find a way to figure out which post is which.
由于我的帖子在另一个表中,因此我找不到找到哪个帖子是哪个的方法。
I've tried this 我已经试过了
SELECT * FROM posts WHERE followed_user=:username ORDER BY post_date DESC
But couldn't figure out a way to use :username
with multiple username. 但是无法找出一种使用
:username
和多个用户名的方法。 Like the followers. 像追随者。 So basically my question is, how can I display multiple posts, from multiple users, on one wall.
因此,基本上我的问题是,如何在一个墙上显示来自多个用户的多个帖子。 Or if anyone can solve how I can select posts from multiple people you're following.
或者,如果有人可以解决我如何从您关注的多个人中选择帖子的问题。
Just to add on when I do SELECT * FROM followers WHERE follow_from = :username
and run 仅当我执行
SELECT * FROM followers WHERE follow_from = :username
时添加,在SELECT * FROM followers WHERE follow_from = :username
并运行
$posts = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$posts[] = array(
'follow_to' => $row['follow_to']
);
}
And do 然后做
foreach($posts as $post) {
echo $post['follow_to'];
}
I get a list of the followers, so I'd assume this will have something to do with retrieval of the posts. 我得到了一个追随者列表,所以我认为这与帖子的检索有关。
You are looking for JOIN/Subquery, 您正在寻找JOIN /子查询,
SELECT
*
FROM
posts
WHERE
post_user IN
(
SELECT follow_to FROM followers WHERE follow_from = :user
)
Update 更新
If you'd like to load a list of posts by a single person as well as the followers, 如果您想加载一个人以及关注者的帖子列表,
SELECT
*
FROM
posts
WHERE
post_user IN
(
SELECT
follow_from
FROM
followers
WHERE
follow_to = :user
OR
follow_from = :user
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.