Solved

Connect friends and user table together

Posted on 2008-10-24
5
699 Views
Last Modified: 2010-04-21
Hello everybody,
I'm running into a problem with developing my site.
People can become friends of one and eachother and I want these people to see their friends a a specific page.

I have two tables:

Table 1: Friends
ID
user_1
state_1
user_2
state_2

Table 2: users
ID
First_Name
Last_Name
etc

People are friends once their ID is in user_1 or user_2 and both states are set to '1'.

Now I need to connect to the database and make a query where it pulls your friends ID and connects this to the users databes to get First_Name, Last_Name, etc.
How do I do this?

One of the problems is that the friends ID can be in column user_1 or user_2, so how do I filter this?

I saw a questions pretty similiar to mine, with a good answer, but just couldn't get this to work!
http://www.experts-exchange.com/Database/MySQL/Q_21498279.html?sfQueryTermInfo=1+friend

Hope you guys can give me some help!
0
Comment
Question by:RonWon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 22798733
I'll propose an answer momentarily, but first I need to question why you have both state_1 and state_2 in the friends table?  That table represents a relationship between two users; it's OK to store the state of the relationship in that table, but it is not so good to store the state of the users in that table.  Can you please clarify what the usage of those columns is?

Now to your question.  I am assuming the "Friends" relationship is bi-directional.  By that I mean participation as user_1 means the same thing as partipating as user_2.  In fact,  a check constraint that user_1 < user_2 would be entirely logical to avoid duplicates or being a friend with yourself.

Anyway, here's the SQL:

Select u.firstname, u.lastname, f.firstname, f.lastname
From
(Select user_1 user, user_2 friend from Friends where status_1=1 and status_2=1
union user_2, user_1 from friends where status_1=1 and status_2=1) r
inner join users u on u.id = r.user
inner join users f on f.id = r.friend
order by u.id









 
0
 

Author Comment

by:RonWon
ID: 22799374
Hey dgmg,
The different states are used as a check for who asked who as friend!
For instance, when Mike (ID: 005) asks Jen (ID: 009) as friend, the table gives:

ID      user_1    state_1    user_2     state_2
1        005           0             009             2

So, on Jen's page, a query searches for user_2 = 009 and state_2 = 2
When Jen accepts the friend request, a form updates the table to both states 1

ID      user_1    state_1    user_2     state_2
1        005           1             009             1

So this means they have both agreed to the friendship!

I have a session variable made upon login called kt_login_id, this is the main ID of the user.
So when I want to select all records where the user is a friend (so 'kt_login_id' in user_1 or user_2 column).

The query I use to do this is:

SELECT *
FROM Friends
WHERE user_1 = '$_SESSION['kt_login_id']' AND state_1 = '1'  OR user_2 = '$_SESSION['kt_login_id']' AND state_2 = '1'

Now it needs to be expanded so that it pulls the user data from table 'users'. This is the user data  from the column where the ID is not $_SESSION['kt_login_id']!

Hope this all makes sense.
I could get your former code to work, so I hope this explanation helps a bit more!!!!!
0
 

Author Comment

by:RonWon
ID: 22799402
The last line must say!!!!!!
I !couldn't! get your former code to work, so I hope this explanation helps a bit more!!!!!

Thanks!

0
 
LVL 42

Accepted Solution

by:
dqmq earned 250 total points
ID: 22801484
Thanks for the clarification of the state codes.  I would have used a single state code to simplify things, but your way will suffice.  The main disadvantage of your way is that you have to double up your logic (check both status codes to find the "asker").   You could just as well ALWAYS let user_1 represent the asker and eliminate one of the state codes.

You must use a UNION to pick up the "friends" relationship in both directions.

SELECT  Users.FirstName, Users.LastName
FROM Friends inner join Users on Friends.User_2 = Users.Id
WHERE user_1 = '$_SESSION['kt_login_id']' AND state_1 = '1'  
UNION
SELECT Users.FirstName, Users.LastName
FROM Friends inner join Users on Friends.User_1 = Users.Id
WHERE user_2 = '$_SESSION['kt_login_id']' AND state_2 = '1'  






0
 

Author Closing Comment

by:RonWon
ID: 31509746
This worked!
Thanks a million!
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question