4 replies
I am trying to do the following requirements for a client:
  1. Add friend.
  2. Remove friend.
  3. Post status updates.
I have sorted out the updates part, but am a bit confused about the database structure for add friends. I will be doing this in PHP. What is the best database structure I can develop so as not to have any duplicate entries in the tables?
#clone #facebook
  • Profile picture of the author Manfred Ekblad
    How about one table for the users, and then one table "friendships" with two user-id's as the primary key.

    When a new friendship is created always put the lowest user id in the first column, that way you only have to look for one matching row if you want to check for duplicates or existing friendship between two users (instead of having an OR in your select query).

    You still have to check both columns for the userid when you want list one specific user's friends, but you would have to do that anyway.
    {{ DiscussionBoard.errors[2088137].message }}
  • Profile picture of the author Shounak Gupte
    i already have users in one table. the primary key is the user_id.

    i didnt quite understand how i can search the friendships table if i want to display all the friends user "10" has. user 10 can be in any of the 2 columns. how will the query work?
    Signature
    Looking for a quality but affordable graphic designer to partner with. To express your interest PM me with some samples.
    {{ DiscussionBoard.errors[2088965].message }}
  • Profile picture of the author Manfred Ekblad
    SELECT * FROM friendships WHERE userid1=123 OR userid2=123

    You will get all the rows where userid 123 exists.
    {{ DiscussionBoard.errors[2089318].message }}
  • Profile picture of the author wayfarer
    Maybe this will help you. It's my entire "Connection" class from a social networking framework I build as a hobby:

    PHP Code:
    <?php
    class Connection {
        protected 
    $user_ids = array(), $following_ids = array(), $user_id;
        
        function 
    __construct($user_id) {
            
    $this->getConnections($user_id);
        }

        public function 
    getConnections($user_id) {//inefficient
            
    $user_ids = array();
            
    $follow_ids = array();
            
    $sql 'select follow_user_id from follow where user_id='.$user_id;
            
    $result fn::sql($sql);
            while(
    $row $result->fetch_assoc()) {
                
    $follow_ids[] = $row['follow_user_id'];//array of who user is following
                
    $sql2 'select user_id from follow where user_id='.$row['follow_user_id'].' and follow_user_id='.$user_id;
                
    //see if follow is reciprocal
                
    $result2 fn::sql($sql2);
                while(
    $row2 $result2->fetch_row()) {
                    
    $user_ids[] = $row2[0];//array of reciprocal follows
                
    }
            }
            
    $this->user_ids $user_ids;//array of reciprocal follows
            
    $this->following_ids $follow_ids;//array of who user is following
            
    $this->user_id $user_id;
            return 
    $user_ids;
        }

        public function 
    isConnected($user_id_connected_to_question) {
            return 
    in_array($user_id_connected_to_question$this->user_ids);
        }

        public function 
    isFollowing($user_id_following_question) {
            return 
    in_array($user_id_following_question$this->following_ids);
        }
        
        function 
    __get($prop) {
            return 
    $this->$prop;
        }
    }
    ?>
    Instead of a "friendship" table, I have a "follow" table. My system does not require mutual following, much like Twitter, but if two users follow each other, they make a "connection".
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[2089540].message }}

Trending Topics