Jump to content

Recommended Posts

Posted

Hello all you smart people :). I'm not sure if this is the best forum topic for this, sorry.

 

I'm having trouble coming up with the SQL logic for what I want to do on my site.

My site is a collection of profiles/bios/pictures, etc..

 

I want to find a way to have my users add 'friends' (other profiles) to their page.

All profiles have a unique INT ID so I'll speak of profiles by number.

So, user #1 wants to add user 2, 3, and 4 as a 'friend'.

 

How should I setup the SQL? The best (only) idea I can come up with is to:

 

For profile #1 add a field called friends

then insert 2

then insert , 3

then insert , 4

and so on..

 

The table would then be [2, 3, 4]

 

For php sql query, do an explode(, ) on the friends filed.

This will break down the different ID's and I can then use them for display or whatever.

 

This, to me, seems sloppy and promises to be difficult to have users remove 'friends'.

 

I'm looking for more logic that actual code. ANY help would be greatly appreciated. Let me know if further explaination is required.

 

Thanks to all.

As always :)

Posted

I'd suggest using a separate table to keep track of the friends. I don't know what information you want to keep track of for each "friendship", but the table could have just a minimum of three fields:

 

1) id - unique record number for this "friends" table

2) user_id - id number from "users" table of user declaring / adding friend

3) friend_user_id - id number from "users" table of friend being added

 

I'd probably index all columns of this table to make searching / locating records faster.

 

To list a user record with their friends, the MySQL query would do a join on the user table and this new "friends" table, joining the "id" field from the user table with the "user_id" field in the "friends" table.

 

Removing a "friend" would just entail deleting the appropriate record from the "friends" table.

 

That's my 2 cents. :)

Join the conversation

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

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...