Solved

Connect friends and user table together

Posted on 2008-10-24
5
693 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
( ! ) Warning: Illegal string offset  Error MYSQL PHP 62 76
SQL Insert Query Help, part2 6 57
Instering to MySQL table 5 38
simple shopping cart database structure 5 31
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now