Welcome Guest, Not a member yet? Register   Sign In
Help with query
#3

By the looks of it you are doing it 1 query by for every friend? That unfortunately creates infamous N+1 issue, if you have 100 friends, you are doing 100 DB queries.

The simplest solution is to get all users you follow, then all users that follow you, then find the overlap in PHP side.

PHP Code:
$following = [];
$followers = [];
$followbacks = [];

// get all users that current user is friends with
$q $this->db->select('friend_id')
    ->
where('user_id'$this->session->userdata('user_id'))
    ->
get($this->relationship);

foreach (
$q->result() as $row) {
    
$following[$row->friend_id] = $row->friend_id;
}

// get all users that follow you
$q $this->db->select('user_id')
    ->
where('friend_id'$this->session->userdata('user_id'))
    ->
get($this->relationship);

foreach (
$q->result() as $row) {
    
$followers[$row->user_id] = $row->user_id;
}

$followbacks array_intersect($following$followers); 
Reply


Messages In This Thread
Help with query - by kirasiris - 09-11-2018, 12:48 AM
RE: Help with query - by php_rocs - 09-11-2018, 05:37 AM
RE: Help with query - by Pertti - 09-11-2018, 08:32 AM
RE: Help with query - by kirasiris - 09-11-2018, 11:57 AM
RE: Help with query - by php_rocs - 09-11-2018, 10:16 AM
RE: Help with query - by kirasiris - 09-11-2018, 12:00 PM
RE: Help with query - by php_rocs - 09-11-2018, 01:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB