dongmaoxi0477 2013-07-18 10:50
浏览 25
已采纳

循环通过Facebook的朋友在数据库中查找匹配,facebook api

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 )....
  • 写回答

1条回答 默认 最新

  • dongzhu7329 2013-07-18 11:13
    关注

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

    foreach ($user_friends['data'] as $row) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺