如何创建Facebook像朋友系统在php与mysql [英] How to create Facebook like friends system in php with mysql

查看:113
本文介绍了如何创建Facebook像朋友系统在php与mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要创建一个像Facebook这样的朋友系统,非常奇怪。当用户添加某人时,该人将被通知,如果同意,他们将是共同的朋友(在这种情况下,意味着双方都是朋友,字面上),或者如果有人离开请求没有相应,他们将成为有人。有人拒绝该请求,他们将不再粉丝或朋友。

I want to create a friends system like facebook, well quite weird than that. When a user adds someone, the someone will be notified, if agree then they'll be mutual friends(in this case, meaning both parties are friends, literally) or if the someone leaves the request unresponded, they'll become fan of the someone. The someone reject the request, they'll no longer fan nor friend.

如何在PHP中使用MYSQL。

How can do this in PHP with MYSQL. Here is my initial planning, somehow im not sure it'll be the best way to do it.

MYSQL TABLE

MYSQL TABLE

user_id (request made by) | friend_id (request point towards) | status
--------------------------------
123     | 452       | 'waiting'
525     | 123       | 'waiting'

含义用户123有来自525的1个朋友请求,并向452发出1个朋友请求。
这里的问题是我如何得到用户123的朋友列表?如果用户123接受525的请求,并且用户452接受123的请求。

Meaning user 123 have 1 friend request from 525, and made 1 friend request to 452. The problem here is how Im going to get the friend list of user 123? if user 123 accept 525's request and user 452 accept 123's request. And also how to get the fans list by MYSQL QUERY..and how to get friends of friend?

推荐答案

用户的朋友列表123:

Friend list of user 123:

SELECT u.* FROM users u INNER JOIN friend_requests f ON f.user_id = u.id WHERE f.user_id = 123 AND status = 'accepted';

用户123是这些用户的粉丝:

User 123 is fan of these users:

SELECT u.* FROM users u INNER JOIN friend_requests f ON f.user_id = u.id WHERE f.friend_id = 123 AND status = 'waiting';

不要忘记在状态栏中添加索引。

Don't forget to add index on the status column.

此外,我不确定这样的表是否理想。

Also, I'm not sure a table like this is ideal for this.

编辑:

我可能会选择这样的模式:

I would probably choose a schema like this:

friend_request
    request_from (foreign key to users.user_id)
    request_to (foreign key to users.user_id)
    resolved (boolean 1 or 0, default is 0)

friend_xref
    friend (foreign key to users.user_id)
    is_friend_with (foreign key to users_user_id)

fan_xref
    user (foreign key to users.user_id)
    is_fan_of (foreign key to users.user_id)

当有人发出朋友请求时,向friend_request表添加新行。当请求收件人选择:

When somebody makes a friend request, add new row to friend_request table. When the request recipient chooses to:


  • 接受请求:在friend_request表中向friend_xref添加新行并将其解析为1

  • 拒绝请求:在friend_request表中向fan_xref添加新行并将其解析为1

但是最好在mysql,数据库设计或一些类似的标签下询问这个,以获得最好的答案。

But it would be better to ask this under mysql, database-design or some similar tag to get best answers.

这篇关于如何创建Facebook像朋友系统在php与mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆