Ok, we’ve created our fancy login system for our site. Now what? How do my members intereact with each other? This bring us back over 5 years ago when MySpace.com first debuted. Every web programmer on earth went “gaga” over how such a simple method used for “friending” another member became so big! Today we have sites like friendster, facebook, and orkut that use the same theory behind virtually friending your friends!
Here’s how they do it:
If Member1 wants to friend Member2, a request is made via a link or action on Member1’s part.
Using a relational database, this is what this request will look like.
Member’s Table (this contains all of our members, it may also contain other information like name, password, and other profile information.)
memberID 1 2
Friend’s Table (This table is updated whenever a member invokes a “friending” action.)
memberID friendID Confirmed 2 1 0
When the member represented by the “friendID” confirms that they would in fact want to be friends with the member represented by “memberID,” then “Confirmed” will be updated to “1” in place of “0.”
So now, our scripts will have a way to tell if one member is friends with another, how many friends any given member has, how many requests for friends a given member has.
MySQL Snippet for querying the number of friends of a given member
SELECT count(*) FROM Friends WHERE memberID = 1 AND Confirmed > 0;