How do I use the result from FB FQL multiquery?

夙愿已清 提交于 2019-11-30 09:20:49

You could match these results up by looping over the comments and matching the fromid to an id from the users response.

For example:

    var comments = response[0].fql_result_set;
    var users = response[1].fql_result_set;    

    //loop through the comments
    for(var i = 0, j = comments.length; i<j; i++){

        for(var x = 0, y = users.length; x<y; x++){
             if(comments[i].fromid == users[x].id){
                 //we have a match, this comment is from user users[x]
                 //process users[x]
                 //break the inner for loop, since we already have a match
             }
        }

    }

If you're working with PHP and need a single array of comments to loop through this function might come in handy:

public function getComments($objectID){
    $user = $comment =array();
    $q1 = "/fql?q=".urlencode("SELECT  id, fromid, text, time , likes FROM comment WHERE object_id ='$objectID' ");
    $res = $this->api($q1);
    $com = $res['data'];

    $q2 = "/fql?q=".urlencode("SELECT uid, name, username, pic_small, current_location FROM user WHERE uid IN (SELECT fromid FROM comment WHERE object_id ='$objectID' )");
    $res = $this->api($q2);
    $usr = $res['data']; 

    foreach($usr as $k=>$v){
        $user[$v['uid']] = $v;
    }
    foreach($com as $cmnt){
        $comment[$cmnt['id']] = $cmnt;
        $comment[$cmnt['id']]['user'] = $user[$cmnt['fromid']];
    }
     return $comment;
}

Returns a single array of comments with the commentID as key:

Array(

[137194739772009_249649] => Array
    (
        [id] => 137194739772009_249649
        [fromid] => 1454592211
        [text] => Brilliant!
        [time] => 1357450854
        [likes] => 1
        [user] => Array
            (
                [uid] => 1454592211
                [name] => Jo Payne
                [username] => jo.payne.127
                [pic_small] => http://profile.ak.fbcdn.net/hprofile-ak-snc6/203035_1454592211_505092710_t.jpg
                [current_location] => Array
                    (
                        [city] => Pascoe Vale
                        [state] => Victoria
                        [country] => Australia
                        [zip] => 
                        [id] => 107340732634422
                        [name] => Pascoe Vale, Victoria, Australia
                    )

            )

    )

[137194739772009_252711] => Array
    (
        [id] => 137194739772009_252711
        [fromid] => 1734247348
        [text] => testing
        [time] => 1357531321
        [likes] => 0
        [user] => Array
            (
                [uid] => 1734247348
                [name] => Andreas Lustig
                [username] => andreaslustigcom
                [pic_small] => http://profile.ak.fbcdn.net/hprofile-ak-snc6/275058_1734247348_2025403101_t.jpg
                [current_location] => Array
                    (
                        [city] => Melbourne
                        [state] => Victoria
                        [country] => Australia
                        [zip] => 
                        [id] => 116190411724975
                        [name] => Melbourne, Victoria, Australia
                    )

            )

    )

)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!