Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the result from FB FQL multiquery?

I'm confused by Facebook's fql.multiquery method.

I'm trying to retrieve all the comments on a post, and then the user information for each one as well. I can get the comments without any problem, but I'm having trouble getting the users.

Currently I'm using the following:

  FB.api({
   method: 'fql.multiquery',
   queries: {
    query1: 'SELECT post_fbid, fromid, text, time FROM comment WHERE post_id="'+postID +'"',
    query2: 'SELECT id, name, url, pic FROM profile WHERE id IN (SELECT fromid FROM #query1)'
   }
  },
  function(response) {
  }
})

This gives me the following response:

[
  {
    "name": "query1",
    "fql_result_set": [
      {
        "post_fbid": "xxxxx",
        "fromid": user1id,
        "text": "Here's a comment",
        "time": 1308579931
      },
      {
        "post_fbid": "xxxxx",
        "fromid": user2id,
        "text": "Another comment",
        "time": 1308580031
      }
    ]
  },
  {
    "name": "query2",
    "fql_result_set": [
      {
        "id": user1id,
        "name": "User 1 name",
        "url": "User 1 url",
        "pic": "User 1 pic"
      },
      {
        "id": user2id,
        "name": "User 2 name",
        "url": "User 2 url",
        "pic": "User 2 pic"
      }
    ]
  }
]

Problem is, I don't know how to match these up! So I'm looping over the comments, and want to print the text of each one with the user's name next to it. How do I do that?

Or, is there a better way to do this?

like image 861
Sharon Avatar asked Jun 23 '11 19:06

Sharon


1 Answers

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
             }
        }

    }
like image 146
Nick DeFazio Avatar answered Oct 24 '22 21:10

Nick DeFazio