Welcome Guest, Not a member yet? Register   Sign In
How to verify if someone is a friend of a friend
#5

[eluser]RMinor[/eluser]
After more research I was able to come up with a solution. Here it is and hopefully it can help someone else. If anybody sees anything wrong with it or can improve upon it feel free to let me know.
Code:
public function friends_of_friends($profile_id, $member_id) {
    // Subquery #1
    $this->db->select('profiles.id')
        ->from('profiles')
        ->join('friends', 'friends.to_id = profiles.id OR friends.from_id = profiles.id', 'left')
        ->where('(to_id = ' . $profile_id . ' OR from_id = ' . $profile_id . ')')
        ->where('profiles.id !=', $profile_id)
        ->where('friends.status', 'approved');
    $first_where_clause = $this->db->get_compiled_select();
    // Subquery #2
    $this->db->select('profiles.id')
        ->from('profiles')
        ->join('friends', 'friends.to_id = profiles.id OR friends.from_id = profiles.id', 'left')
        ->where('(to_id = ' . $member_id . ' OR from_id = ' . $member_id . ')')
        ->where('profiles.id !=', $member_id)
        ->where('friends.status', 'approved');
    $second_where_clause = $this->db->get_compiled_select();
    // Main query
    $this->db->select('COUNT(*) AS count')
        ->from('profiles')
        ->where('profiles.id IN (' . $first_where_clause . ')', null, false)
        ->where('profiles.id IN (' . $second_where_clause . ')', null, false);
    $query = $this->db->get();
    return ($query->num_rows() > 0) ? $query->result() : false;
}

A quick note, I had to add a method to the system/database/DB_active_rec.php file in order for my query to work. That method is below.
Code:
public function get_compiled_select($table = '', $reset = TRUE) {
    if ($table != '') {
        $this->_track_aliases($table);
        $this->from($table);
    }
    $select =  $this->_compile_select();
    if ($reset === TRUE) {
        $this->_reset_select();
    }
    return $select;
}


Messages In This Thread
How to verify if someone is a friend of a friend - by El Forum - 08-19-2013, 09:05 PM
How to verify if someone is a friend of a friend - by El Forum - 08-19-2013, 09:47 PM
How to verify if someone is a friend of a friend - by El Forum - 08-19-2013, 10:23 PM
How to verify if someone is a friend of a friend - by El Forum - 08-20-2013, 02:57 PM
How to verify if someone is a friend of a friend - by El Forum - 08-22-2013, 05:27 PM



Theme © iAndrew 2016 - Forum software by © MyBB