2

好的,我有两张表,一张名为account_members,另一张名为account_follows. 我想要一个 Twitter 风格的关注系统,其中 account_members 可以互相关注。

Account Follows Table Structure:

id
account_name
followed_name
time

Account Members Table Structure:

id
Account_name
status (Account active or not)

我想我可以通过一个简单的查询来获得所有被关注的帐户:

public function following($account_name)
{
    $sql = "SELECT 

    F.id, F.account_name, F.followed_name, F.time, 
    M.account_name AS session_name, M.status 

    FROM account_follows F
    LEFT JOIN account_members M ON F.account_name = M.account_name

    WHERE F.account_name = :account_name 
    AND M.account_name = :account_name

    ORDER BY id DESC LIMIT 5";
}

这将显示所有正在关注的 account_members($account_name通过 url 设置)

我遇到的问题是允许登录的 account_member 能够关注或取消关注他们关注的朋友的朋友。我通过执行以下操作对登录的 account_member 进行简单检查,以取消关注其列表中的任何人:

if($_SESSION['account_name'] == $row['account_name'])
{
    echo'<a href="" id="..." class="...">Unfollow</a>';
}

以上工作正常,但我想对登录的帐户关注者关注者做类似的事情......如果这有意义吗?

所以Bob已登录,Bob查看他的关注列表并单击mike并查看mike关注的人,并且从该列表中可以关注/取消关注mike关注的人(其中一些 Bob 可能关注)

任何帮助或指导表示赞赏。

4

1 回答 1

1

您的查询将适用于传入的任何成员的帐户名,但查询本身不考虑当前登录的成员的关注,因此您需要将他们的数据加入其中。

该查询返回指定帐户的 url 所关注的成员列表。这有点告诉登录用户是否也在关注该成员。使用该位来决定您是否需要回显关注或取消关注链接。

SELECT 
        theirFollows.id, theirFollows.account_name, 
        theirFollows.followed_name, theirFollows.time, 
        M.account_name AS member_name, M.status, 
        case 
            when myFollows.followed_name is null then 0
            else 1
        end as sessionMemberIsFollowing
FROM    account_members M
        LEFT JOIN account_follows theirFollows
          ON theirFollows.account_name = M.account_name
        LEFT JOIN 
            (
                select followed_name
                from account_follows 
                where account_name = :session_account_name
            ) myFollows
            on myFollows.followed_name = theirFollows.followed_name

WHERE   M.account_name = :account_name

您选择的列之一被标记为 session_name,但这有点误导,因为传入的 account_name 来自 url。此外,您只需要其中一个 where 子句,因为那是您要加入的列。

于 2013-03-19T13:27:51.017 回答