如何在PHP的foreach循环中组合两个查询?

时间:2012-03-22 06:03:58

标签: php facebook facebook-fql

Facebook API可以为用户管理员提供页面。假设它返回以下数组。不幸的是,它不会返回它的名称或网址。

这会转到我的页面ID列表。

$results = $facebook->api('/fql', array('q'=>'SELECT page_id, type FROM page_admin WHERE uid = me()'));

foreach($results['data'] as $result) {
    echo $result['page_id'], '<br />';
}

使用以下内容我会回显页面ID的名称。

$pagename = $facebook->api('/fql', array('q'=>'SELECT name FROM page WHERE page_id = 230127303706132'));
echo $pagename['data'][0]['name'];

我的问题是如何以有效的方式列出页面名称?
我的猜测是我必须将查询放在foreach循环中。

2 个答案:

答案 0 :(得分:2)

try this

$fql=https://graph.facebook.com/fql?q=SELECT name FROM page WHERE page_id in(SELECT page_id, type FROM page_admin WHERE uid = me())&access_token=xxxxxxxx;
$page_names =json_decode(file_get_contents($fql));
print_r($page_names);
It will show an array of page names.

答案 1 :(得分:0)

首先抓住page_ids

$results = $facebook->api('/fql', array('q'=>'SELECT page_id, type FROM page_admin WHERE uid = me()'));

$page_ids = array();
foreach($results['data'] as $result) {
    $page_ids[] = $result['page_id'];
}

然后获取页面名称

$pagenames = $facebook->api('/fql', array('q'=>'SELECT name FROM page WHERE page_id IN (' . join(',', $page_ids) .')' ));

// list all page names
foreach($pagenames['data'] as $result) {
    echo $result['name'], '<br />';
}

在一个查询中获取

$pagenames = $facebook->api('/fql', array('q' => 'SELECT name FROM page WHERE page_id IN (SELECT page_id FROM page_admin  WHERE  uid=me())'));

// list all page names
foreach($pagenames['data'] as $result) {
    echo $result['name'], '<br />';
}