Wanting to create a "buddy list" script...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kandieman101
Forum Newbie
Posts: 2
Joined: Thu Jul 22, 2004 4:23 pm

Wanting to create a "buddy list" script...

Post by kandieman101 »

Okay, so I am wanting to create a script, and the best way I can think is to call it a buddy list.

I have no problems with any aspect, except for saying who is who's buddy. What would be the best way to accomplish this? One person cannot be a friend of another, without the other being a friend of that person...

I can't quite think of how I would link them together. I first thought of like another table called friends which had USER_ID and FRIEND_ID... then just get all FRIEND_IDs for the current USER_ID... however, if user's 1 and 2 were friends.. in that table I would have
USER_ID = 1
FRIEND_ID = 2

USER_ID = 2
FRIEND_ID = 1

that seems rather redundant and surely there must be a better way. I think basically all IM programs accomplish this because I think each person has to be on each other's lists... so how can I cross reference the two user ids that they are FRIENDS OF EACH OTHER??

I hope I explained that well enough... what im thinking is hard to write down hehe..

Thanks in advance!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

now if you a table friend with person1_id, person2_id and the primary key is person1_id, person2_id you don't have to/can't store both (a,b) and (b,a)


[untested]
get friends in 1st grade

select *
from people AS p, friend AS f
where p.people_id=f.person1_id or p.people_id=f.person2_id

get friends in 2nd grade

select *
from people AS p, friend AS f, friend as f2
where p.people_id=(f.person1_id and (f.person2_id=f2.person2_id or f.person2_id=f2.person1_id)) or (p.people_id=f.person2_id and (...))
Post Reply