dongmaoxi0477 2013-07-18 02: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:

  1. public function index() {
  2. if ($this->user) {
  3. try{
  4. $data['user_profile'] = $this->facebook->api('/me');
  5. $data['friends_list'] = $this->Users_Model->get_friends_on_drimmly($this->facebook->api('/me/friends'));
  6. } catch(FacebookApiException $e) {
  7. error_log($e);
  8. $this->user = null;
  9. }
  10. }
  11. $data['logout'] = $this->facebook->getLogoutUrl(array('next' => base_url().'login/logout'));
  12. $this->load->view('dashboard', $data);
  13. }

users_model:

  1. public function get_friends_on_drimmly($user_friends) {
  2. $return_array = array();
  3. foreach ($user_friends as $friend => $row) {
  4. $query = $this->db->select()->from('users')->where('id', $row['id'])->get();
  5. if ($query->num_rows() != 0){
  6. foreach ($query->result_array() as $result => $row) {
  7. $return_array[] = array(
  8. 'name' => $row['full_name'],
  9. 'id' => $row['id']
  10. );
  11. }
  12. }
  13. }
  14. return $return_array();
  15. }

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 03:13
    关注

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

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部