Page 1 of 1

Help with friends system.

Posted: Tue Oct 14, 2008 7:29 am
by Aldur18
Hey guys,
I'm new here so please move this post to a more correct location if I have posted in the wrong area.
I couldn't see a more suitable area, but I am tired so I may have overlooked something.

Anyway my problem is, I have a friends system for my basic social networking site and I'm having trouble with a couple of things.

1. When a user requests to be friends with another, that user should not be able to request again - this doesn't happen.
The message 'You are already friends with this user' appears, or sometimes it sends a second request. (Code attached below)

2. When two users become friends they should not be able to request to be friends either, again this doesn't happen.

This really has me stumped and I don't want to add more to the friend feature until I have this straightened out.

Any help would be greatly appreciated!

Thankyou in advance,

Aldur.

Code: Select all

 
$email=($_SESSION['email']);
$email2=($_SESSION['email2']);
 
$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `by` = '$email' ");
$getarray = mysql_fetch_array($get);
$by = $getarray[by];
$by2 = $getarray[email];
 
$get2 = mysql_query( "SELECT * FROM `friends` WHERE `friendemail` = '$by' ");
$get2array = mysql_fetch_array($get2);
$e1=$get2array[email];
$e2=$get2array[friendemail];
 
$get3 = mysql_query( "SELECT * FROM `users` WHERE `email` = '$by' ");
$get3array = mysql_fetch_array($get3);
$name=$get3array[name];
$surname=$get3array[surname];
 
 
if ($email2==$email){
echo "You can't add yourself!";
exit;
}
if($email2 == $e1 or $email == $e2){
echo "You are already friends with this user!<br />";
exit;
}
if($email == $by or $email2 == $by2){
echo "You have already requested to be this user's friend!<br />";
exit;
}
else{
if ($_SESSION['email2']){
$email = htmlspecialchars($_SESSION['email2']);
$by = $_SESSION['email'];
$query = mysql_query("INSERT INTO `friend_requests` ( `email` , `by` ) VALUES ( '$email' , '$by' )");
echo ( "$name $surname has been sent a request you must now wait for it to be accepted" );
} else {
echo ( "No request was made" );
}
}
unset($_SESSION['email2']);
 

Re: Help with friends system.

Posted: Tue Oct 14, 2008 11:08 am
by aceconcepts
Sounds like you need a seperate in order to keep track of who's friends with who.

For example you could create called "tblWhoIsFriends", create a few fields i.e. intFriendId1, intFriendId2, dateFriends, timeFriends etc...

Very crude indeed but may be of some help :D

Re: Help with friends system.

Posted: Tue Oct 14, 2008 11:21 pm
by Aldur18
The tables I have now are fine and seems to work fine, I think it's just the way i'm writing it in php that's the problem.

I need it to check the table 'friend_requests' to see if the user has already sent a request to the other user and I also need it to check the table 'friends' to see if they are already friends.

I have made a bit of progress on it but I am at school so I can't post my updated script.

Thanks again guys!

Aldur.

Re: Help with friends system.

Posted: Wed Oct 15, 2008 5:56 am
by aceconcepts
Can you post or table structure?

Re: Help with friends system.

Posted: Wed Oct 15, 2008 8:39 am
by Stryks
Maybe I don't get the complexities involved here, but do you really need two tables to manage friendships?

Why not treat the friends list as ... say ... a personal contact list, as opposed to a table link. Each row of the table belongs to the user, regardless of whether or not the other person has accepted, but set a status to determine the behavior of that contact.

So like ... ID, owner, contact, status

So, assuming that you are using emails to link them (I would use autonumber ID's), Jim sends a request to fred, you add:
jim@host.com, fred@host.com, 1

When fred logs in, you could check for friend requests with ...

Code: Select all

SELECT ID, owner FROM friends WHERE contact = 'fred@host.com' AND status=1;
He sees that the request is from the owner - jim@host.com.

If he says no ...

Code: Select all

DELETE FROM friends WHERE ID = ID(from previous query);
(or maybe you'd want to introduce a third status for rejected. This could be used to trigger a message to the requester letting them know before triggering a delete.

If he says yes you add another row to friends: fred@host.com, jim@host.com, 0 ... and then you update the status on jims row to 0.

Sure the friendship now has two rows to define the friendship ... but that really doesn't matter too much.

Anyhow ... you'd pull a users friend list with ...

Code: Select all

SELECT ID, owner, status FROM friends WHERE owner='user@host.com' ORDER BY status;
When you output the list, friends with a status of 0 do whatever you want .. show online status, message links, whatever. Contacts with status of 1, no detail and a message along the lines of 'friendship request pending'.

Then your problems are easily solved.

Has user already requested: SELECT ID, status FROM friends where owner='me@mine.com' AND contact='you@yours.com'; Record found? Status 0 - you are already friends with this person. Status 1 - You have a pending request. Not found - we might be good to go, one test left.

Has the user got a request with me: SELECT ID FROM friends where contact='me@mine.com' AND owner='you@yours.com'; Record found, request is pending. No record, you're all clear to add.

You could easily merge the two and manipulate the results to find out what is going on. Either way, no results means the request is right to add.

Anyhow ... This is probably just late night waffle, but maybe there is something here you can use. Failing that, post your table structure and we'll see.

Cheers

Re: Help with friends system.

Posted: Thu Oct 16, 2008 4:03 am
by Aldur18
Hey thanks for the replies!

@ Strykes:

You have solved my problems. Thanks for the detailed response, it gave me an idea which I implemented and is now working!

You truly are my hero!

Thanks for your input.

Aldur.