PHP - Help With Friends System

masterjake

New Member
Messages
73
Reaction score
0
Points
0
Does anyone know how to do a friends system. This is how I was going to setup mine but doing it this way I will need a little help.

When you add a friend it updates a database entry with your friends and adds exactly this "#friend,". I do it this way because some people have spaces in there name but no one has a # in their name as its forbidden in my registration script. Now when the time comes to view there friends I need it to go into that entry, break up all of the friends removing the #'s and ,'s and then add them into an array so I can do sql querys on the array and get the id of each friend so I can put a link to their profiles on the view friends page. Can someone help?
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
First, use an auto_increment integer for a unique key for each user. This is called the primary key and is used to link rows to each other.

To create a friend list, simple create your user table and then create a friend table with columns userID and friendID. Place the users ID number (that is, the one that is created through auto_increment) in userID and the friends ID number in friendID. And there you go. One friend list :)
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Does anyone know how to do a friends system. This is how I was going to setup mine but doing it this way I will need a little help.

When you add a friend it updates a database entry with your friends and adds exactly this "#friend,". I do it this way because some people have spaces in there name but no one has a # in their name as its forbidden in my registration script. Now when the time comes to view there friends I need it to go into that entry, break up all of the friends removing the #'s and ,'s and then add them into an array so I can do sql querys on the array and get the id of each friend so I can put a link to their profiles on the view friends page. Can someone help?

xmakina is right - the array system is too cumbersome.

When you want to show all the friends for the user signed in, you just create a recordset where the userid == the uniqueid of the person logged in.
 

masterjake

New Member
Messages
73
Reaction score
0
Points
0
Yeah but I don't want to create an individual table for each user, that get's too annoying and large to keep up with, i'd rather do the arrays.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Yeah but I don't want to create an individual table for each user, that get's too annoying and large to keep up with, i'd rather do the arrays.

You don't need to - all you need is one friends table - the links between your existing user table and the friends table will do the work for you.

When you click on add friend, you simply insert a record to the friends table, carrying the unique ID from the user table for the logged in user and the unique ID of the "friend".

recordset:

SELECT * FROM FRIENDS WHERE LINKUSERID LIKE $_SESSION['userid']

This will show all friends for the selected user. (based on session but can be from a link or post.)
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
SELECT * FROM FRIENDS WHERE LINKUSERID = $_SESSION['userid']

We're dealing with numbers and don't want to risk user 1 getting user 11's friends.

And Jake, I highly recommend you read up on some database theory before plunging into something like this. You don't sound very clued up on how a database should be structured and organised and without those critical foundations your structure will quickly tumble down.
 

masterjake

New Member
Messages
73
Reaction score
0
Points
0
Naww its cool ;] thanks for the replies I've been using DB's quite a well I understand what they mean by the friends table now.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
SELECT * FROM FRIENDS WHERE LINKUSERID = $_SESSION['userid']

We're dealing with numbers and don't want to risk user 1 getting user 11's friends.
quote]

Interesting - I thought this was comparing two absolute values ??

I obviously need to read up more on numbers.. ;)
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
Interesting - I thought this was comparing two absolute values ??

I obviously need to read up more on numbers.. ;)

He is; read the post above his.

SELECT * FROM FRIENDS WHERE LINKUSERID LIKE $_SESSION['userid']

He changed Like to =; like would match 1 to 1, 11, 111, 1111, and if I'm not mistaken numbers like 1245 as well. = makes it absolute like you pointed out, which is what we needed :)
 
Last edited:

xmakina

New Member
Messages
264
Reaction score
0
Points
0
It's not so much that it *DOES* match 1 to 11, 111, 1111 and so on as it *MIGHT*. Sure in MySQL it doesn't because it uses wildcards and symbols (e.g. LIKE '1*'), but who knows what variations of SQL will come along. One might think the use of LIKE means *STRING* and therefore 1 will match 1, 12, 113, etc etc.
 
Top