I am stuck on the issue of friends list
i have two tables First
1- Users table (ID, username, Password)
2- Friends table (id, myid, friend_id)
now if i have two users in users table - First user id = 50 - Second user id = 56
and at same time i have in the friends table one record ID=1 myid=50 friend_id = 56
i want to create query and this query ( if i login and my id is 50 ) it will show me only username of 56 user and also if i login with user id 56 it will show me the username of 50 user
i know there will be two query first will bring all my friends with my id and second will show my friends without my id but i don't know how pls help
some if my code
function get_myfriends(){
$this->db->where('myid',$this->session->userdata('userid'));
$this->db->or_where('fid',$this->session->userdata('userid'));
$query = $this->db->get('myfriends');
return $query->result();
}
function get_user_by_id($id){
$this->db->where('userid',$id);
$query = $this->db->get('users');
foreach ($query->result() as $row){
// Here is the problem
if($row->myid == $this->session->userdata('userid'))
}else{
}
return $query->result();
}
I think you should make friends relation in both sides, for example if user 50 and user 56 are both friends to establish this relationship you should insert 2 entries in myfriends table:
+------+-----+
| myid | fid |
+------+-----+
| 50 | 56 |
| 56 | 50 |
+------+-----+
and then to get all friends for any user you can simply do it by:
$sqlQuery = 'SELECT * FROM myfriends
JOIN users
ON myfriends.fid = users.userid
WHERE myfriends.myid = ?';
return $this->db->query($sqlQuery, $id)->result();
Update
if you want it only one side relation you can try this query:
$sqlQuery = 'SELECT * FROM myfriends
JOIN users
ON (myfriends.fid = users.userid OR myfriends.myid = users.userid) AND user.userid <> ?';
return $this->db->query($sqlQuery, $id)->result();