tags:

views:

87

answers:

3

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.

In my user table, i have user_id, first_name, last_name, email, etc.

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!

+1  A: 

something like this:

create table users(
 user_id int unsigned not null auto_increment primary key,
 username varbinary(32) not null
)engine=innodb;


create table user_friends(
 user_id int unsigned not null,
 friend_user_id int unsigned not null,
 primary key (user_id, friend_user_id)
)engine=innodb;

to display a user's set of friends:

select
 u.*
from
 user_friends uf
inner join users u on uf.friend_user_id = u.user_id
where
 uf.user_id = 1;
f00
+3  A: 

Well, let's try to keep this simple. You're trying, essentially, to find a way to connect two users together.

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).

Well, so let's say my user_id is 5. Your user_id is 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';

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.

Isaac
to be clear - this scheme more like twitter model with followers/followings, not facebook one with "real" friends. i mean at facebook we get 2-side relation when each user have to add a friend (i don't know the implementation but i bet it's 2 records adding into friends table) before real relation is created.
zerkms
you should be able to reverse the query like: SELECT `user_id` FROM `users_friends` WHERE `friend_id` = '5'
silent
many thanks! I'll implement this idea!
ggfan
A: 

To complement f00 and Isaac answer:

  1. basic pseudo-code would be like this:
  2. have button / URL in your profile page (maybe http://example.com/makefriend.php?memberid=$memberid). Let's call it 'make friend'
  3. makefriend.php will then check memberid from that particular profile page.
  4. makefriend.php will too get requesterid from requester (this is usually saved on session).
    • if empty, maybe not logged in.
  5. Next, makefriend.php will insert memberid and requesterid to, says, friendlist.
silent
2 should be sent via POST with csrf-token otherwise it's pretty simple to friendify anyone using CSRF-attack
zerkms