[eluser]RMinor[/eluser]
I am trying to create a friend request system for a social network I am building. Currently I have two tables, friends and friend_requests. When a member sends a friend request to another member a record is inserted into the friend_request table with the sender id going into the from_id column and the receiver id going into the to_id column. That's all working fine right now. The problem comes when I try to retrieve all of a member's friends for them to view. When a member approves a friend request I insert a record into the friends table with the to_id (which would be the id of the person approving the request) going into the profile_id column and the from_id (which is the id of the sender) going into the friend_id column. When a member wants to view all of their friends I need to be able to select all records with either a friend_id or a profile_id equal to that of the id of the member who is trying to view all of their friends. I think I need to join the friends table twice on the profiles table but I am not sure of the correct query to do that. I am using CodeIgniter 2.1.4 to build this web site so my current attempt looks like the following:
Code:
public function get_all_friends($profile_id) {
$this->db->select('profiles.*');
$this->db->join('users', 'friends.profile_id = profiles.id OR friends.friend_id = profiles.id', 'left');
$this->db->where('profile_id', $profile_id);
$this->db->or_where('friend_id', $profile_id);
$query = $this->db->get('profiles');
return ($query->num_rows() > 0) ? $query->result() : false;
}
Thank you very much in advance.