问题描述
我创建了一个网站,人们可以在其中创建个人资料.但是我试图弄清楚如何开始创建添加朋友"按钮,以便用户可以拥有朋友.
I have created a site where people can create a profile. But I am trying to figure out how to start on making an add friend button so users can have friends.
在我的用户表中,我有user_id,first_name,last_name,email等.
In my user table, i have user_id, first_name, last_name, email, etc.
我应该以某种方式在朋友表中关联用户的user_id和朋友吗?
Should I somehow relate the user_id of the user and the friend in a friend table?
我是编程的新手,所以这些东西对我来说仍然是新的.谢谢!
I am a novice to programming, so these things are still new to me. Thanks!
推荐答案
好吧,让我们尝试保持简单.本质上,您正在尝试找到一种将两个用户连接在一起的方法.
Well, let's try to keep this simple.You're trying, essentially, to find a way to connect two users together.
由于我试图使事情保持简单,并且绝对不暗示这是最好的方法,因此,我认为实现此目的的最简单方法是创建一个新表(users_friends),其中包含以下字段:(user_id)和(friend_id).
Since I'm trying to keep things simple, and definitely not implying that this is the best way of doing it, I think the easiest way to go about doing this is to create a new table (users_friends) with the following fields: (user_id) and (friend_id).
好吧,假设我的user_id是5.您的user_id为10.
Well, so let's say my user_id is 5.Your user_id is 10.
我想将您添加为我的朋友,因此,我将使用以下值向该新创建的表中添加一个条目:user_id = 5,friend_id = 10.
I want to add you as my friend, therefore I'd add an entry to that newly created table with the following values:user_id = 5, friend_id = 10.
因此,假设您想显示我所有的朋友,则可以运行以下查询:
So, let's say you want to display all of my friends, you could run a query such as:
SELECT * FROM `users` WHERE `user_id` IN ( SELECT `friend_id` FROM `users_friends` WHERE `user_id` = '5' );
果然,删除朋友很容易,您所要做的就是从新创建的表中删除条目...
Sure enough, removing a friend is easy, all you have to do is delete the entry from the newly created table ...
DELETE FROM `users_friends` WHERE `user_id` = '5' AND `friend_id` = '10';
po,你突然不再是我的朋友了;)
And poof, you're suddenly not my friend anymore ;)
是的,这些是基础.在尝试一种可以为您带来更大灵活性的解决方案之前,我会先尝试该解决方案.
So yeah, these are the basics.I'd try this solution before moving on to a solution which will allow you more flexibility.
这篇关于如何在PHP中添加添加好友/解除好友功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!