Jump to content

Help with a friends script


Jewbilee

Recommended Posts

Im having a little trouble with this.  I need a way to keep track of friends of users similar to myspace.  One user can add several others to a friends list and be viewed and removed and what not.  The only problem Im having is how to do I store this in a database?  Should I make a seperate table or store the id's of friends in a users table?
Link to comment
Share on other sites

make a seperate table 2 feilds user & friend.  use the user_id feild of your user table. 
To find friends of a particular user
[b]SELECT * FROM tbl_friend WHERE user = '$user_id';[/b].
That will give you a list of the users friends. 
You could find the Name or whatever from your user table by doing something like this:
[b]SELECT first_name, last_name FROM users WHERE user_id IN (SELECT * FROM tbl_friend WHERE user = '$user_id')[/b]

My syntax might be a little off but it's in the ballpark.

-John
Link to comment
Share on other sites

You could try a table like

user_id, friend_id

And then lets say theyre 5 users, corbin, bob, john, frank, and joe.  They have the ids 1 2 3 4 5.

Lets say corbin has bob and joe as friends and bob has joe and frank

the db would look like

1,2
1,5
2,5
2,4

Then assuming you are pulling usernames from the table accounts and this from friends you could do something like the following to pull corbin's friends.

SELECT *.friends,id.accounts,user_name.accounts FROM friends,accounts WHERE user_name.accounts = 'corbin' friends.user_id = id.accounts
Link to comment
Share on other sites

heres the problem with the above parts.. since one field is named user_id and the other is friend.. what if i wanted to display friend's friends and not user_id's...

like..

say:

bob & fred
bod & burt
burt & fred..

you see?  How would i display all of burts friends?
Link to comment
Share on other sites

Hmmm what it does is it pulls everything from the accounts and friends tables and treats it like one giant result set.

Then it would pull the data from accounts where the column user_name is burt  Then it would use the id pulled from the accounts table and use it for checking the id against the user_id and friends_id fields in the friends table.
Link to comment
Share on other sites

You might try a database consisting of 2 columns
my_id, friend_id

Add
----
$aFriend = "INSERT INTO `user_friends` (`my_id`, `friend_id`) VALUES ('".$myID."', '".$myFriendsID."')";
$addFriend = mysql_query($aFriend);
  if(!$addFriend) die(mysql_error());

List Friends
-----------
echo "<table>";
$listFriends = mysql_query("SELECT * FROM `user_friends` WHERE `my_id` = '".$myID."'");
while ($myFriends = mysql_fetch_object($listFriends)) {

$getDetails = mysql_query("SELECT * FROM `user_details` WHERE `id` = '".$myFriends->friend_id."');
$getFD = mysql_fetch_object($getDetails);

echo "<tr><td><a href=\"?page=profile&id=".$getFD->id."\">".$getFD->username."</td></tr>";

}
echo "</table>";

--------------

This code should work.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.