I'm confused by Facebook's fql.multiquery method.
I'm trying to retrieve all the ments on a post, and then the user information for each one as well. I can get the ments 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 ment 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 ment",
"time": 1308579931
},
{
"post_fbid": "xxxxx",
"fromid": user2id,
"text": "Another ment",
"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 ments, 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?
I'm confused by Facebook's fql.multiquery method.
I'm trying to retrieve all the ments on a post, and then the user information for each one as well. I can get the ments 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 ment 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 ment",
"time": 1308579931
},
{
"post_fbid": "xxxxx",
"fromid": user2id,
"text": "Another ment",
"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 ments, 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?
Share Improve this question asked Jun 23, 2011 at 19:02 SharonSharon 3,92916 gold badges66 silver badges106 bronze badges2 Answers
Reset to default 10You could match these results up by looping over the ments and matching the fromid to an id from the users response.
For example:
var ments = response[0].fql_result_set;
var users = response[1].fql_result_set;
//loop through the ments
for(var i = 0, j = ments.length; i<j; i++){
for(var x = 0, y = users.length; x<y; x++){
if(ments[i].fromid == users[x].id){
//we have a match, this ment 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 ments to loop through this function might e in handy:
public function getComments($objectID){
$user = $ment =array();
$q1 = "/fql?q=".urlencode("SELECT id, fromid, text, time , likes FROM ment WHERE object_id ='$objectID' ");
$res = $this->api($q1);
$ = $res['data'];
$q2 = "/fql?q=".urlencode("SELECT uid, name, username, pic_small, current_location FROM user WHERE uid IN (SELECT fromid FROM ment WHERE object_id ='$objectID' )");
$res = $this->api($q2);
$usr = $res['data'];
foreach($usr as $k=>$v){
$user[$v['uid']] = $v;
}
foreach($ as $cmnt){
$ment[$cmnt['id']] = $cmnt;
$ment[$cmnt['id']]['user'] = $user[$cmnt['fromid']];
}
return $ment;
}
Returns a single array of ments with the mentID 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/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] => andreaslustig
[pic_small] => http://profile.ak.fbcdn/hprofile-ak-snc6/275058_1734247348_2025403101_t.jpg
[current_location] => Array
(
[city] => Melbourne
[state] => Victoria
[country] => Australia
[zip] =>
[id] => 116190411724975
[name] => Melbourne, Victoria, Australia
)
)
)
)