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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line