Archive for the ‘PHP’ Category

 34323 Get Mutual Friend list and count using User Relationship Module 

1. Install User Relationship Module 

2. Function to get Mutual Friend List

  • Pass to parameters
    • Requester ID ($requester_id)
    • Requestee ID ($requestee_id)
function common_friend_list($requester_id , $requestee_id)
{

      $result = db_query('SELECT *
                                  FROM {user_relationships} ur1
                                  WHERE ur1.requester_id = '.$requestee_id.'
                                  AND ur1.approved = 1
                                  AND (ur1.requestee_id
                                  IN (SELECT ur2.requestee_id
                                       FROM {user_relationships} ur2
                                       WHERE ur2.requester_id = '.$requester_id.'
                                       AND ur1.approved = 1))
                         ')->fetchAll();

      return $result;
}

 

3. Function Call

$result = common_friend_list($requester_id , $requestee_id);

if(!empty($result))
{
      foreach($result as $row)
      {
            $requestee = user_load($row->requestee_id);
            echo "Mutual Friend : " . $requestee->name; 
      } 
}

 

4. Function to get Mutual Friend Count

  • Pass to parameters
    • Requester ID ($requester_id)
    • Requestee ID ($requestee_id)
function common_friend_count($requester_id,$requestee_id)
{

      $result = db_query('SELECT count(ur1.rid) AS count
                                   FROM {user_relationships} ur1
                                   WHERE ur1.requester_id = '.$requestee_id.'
                                   AND ur1.approved = 1
                                   AND (ur1.requestee_id
                                   IN (SELECT ur2.requestee_id
                                       FROM {user_relationships} ur2
                                       WHERE ur2.requester_id = '.$requester_id.'
                                       AND ur1.approved = 1))
                         ')->fetchAll();

      return $result;
}

 

5. Function Call

$result = common_friend_count($requester_id , $requestee_id);

if(!empty($result))
{
      $result = (array)$result[0];
      echo "Mutual Friends Count : " . $result['count'];
}
Advertisements