hello more knowledgeable horde. very new here, trying to write a simple ‘friends’ query. i have searched the SO base and couldn’t divine the answer from the other questions/solutions.
i have two seemingly standard tables, one for membership with id/firstname/lastname etc, the other a friendship table with id/inviter_id/friend_id/status. i have chosen (arbitrarily) to have friendship structured as one row per relationship rather than two rows per relationship. (seems as though this problem would be easier / non-existent with two rows per, but feels like it would cause other problems and would like to solve it with one if possible.)
i am trying to return an array of firstname/lastname for all the people who are friends with the current user, and am mostly succeeding except i am also getting the user himself listed for each find, so 8 rows includes 4 of the current user e.g. here is my codeigniter query:
$id = $this->session->userdata('id');
$this->db->select('first_name, last_name')->from("members");
$this->db->join('friendship', 'friendship.id_inviter = members.id OR friendship.id_friend = members.id');
$where1 = "friendship.status = 'accepted' AND friendship.id_friend = $id";
$where2 = "friendship.status = 'accepted' AND friendship.id_inviter = $id";
$this->db->where($where1);
$this->db->or_where($where2);
$query = $this->db->get();
return $query;
i imagine it’s some elegant finesse i just can’t see. any thoughts? thanks in advance.
Try adding
"members.id != $id"to your where clause.