Question

I have made a login with facebook that adds the user to my database if they haven't been added yet. I want to show other facebook friends (of the current user that just signed up) that have signed up for my app as well.. I am trying to do this by looping through the facebook friends using $this->facebook->api('/me/friends') and finding matches in my database (by id) and storing those in an array that is sent to a view (code igniter)

But So far I havent been able to loop through the users friends so easily. It keeps coming up with,

Undefined index: id

Here is my code:

dashboard controller:

public function index() {
        if ($this->user) {
            try{
                $data['user_profile'] = $this->facebook->api('/me');
                $data['friends_list'] = $this->Users_Model->get_friends_on_drimmly($this->facebook->api('/me/friends'));

            } catch(FacebookApiException $e) {
                error_log($e);
                $this->user = null;
            }
        }
        $data['logout'] = $this->facebook->getLogoutUrl(array('next' => base_url().'login/logout'));
        $this->load->view('dashboard', $data);
    }

users_model:

public function get_friends_on_drimmly($user_friends) {
    $return_array = array();

    foreach ($user_friends as $friend => $row) {

        $query = $this->db->select()->from('users')->where('id', $row['id'])->get();
        if ($query->num_rows() != 0){
            foreach ($query->result_array() as $result => $row) {
                $return_array[] = array(
                    'name' => $row['full_name'],
                    'id' => $row['id']
                );
            }
        }
     }
    return $return_array();
}

UPDATE: print_r($user_friends):

Array ( [data] => Array ( [0] => Array ( [name] => Daniella Caspers [id] => 6710741 ) [1] => Array ( [name] => Alex Dos Santos [id] => 10029005 )....
Was it helpful?

Solution

With that $user_friends data you need to adjust your foreach to:

foreach ($user_friends['data'] as $row) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top