Reputation: 8836
With the following code I get all the pages I admin on Facebook using FQL. I do not print them though.
$PageNames = $facebook->api('/fql', array('q' =>
'SELECT name, page_id FROM page WHERE page_id IN (
SELECT page_id FROM page_admin WHERE uid=me() )'));
Previously, I have added in my DB the *page_id* of some of my pages. My goal is to show the pages that I have not added to my DB yet. It is working correctly and the code is below.
foreach($PageNames['data'] as $PageName) {
$investigate_id = mysql_query("SELECT page_id FROM pages WHERE page_id='"
.$PageName['page_id']."' LIMIT 1 ");
if(mysql_num_rows($investigate_id) == 0) {
echo $PageName['page_id'].$PageName['name'];
}
}
My problem/question is if somehow I can avoid the numerous DB calls because it makes the query for every page I admin. How can I achieve this?
Upvotes: 2
Views: 118
Reputation: 25938
You may collect the IDs from results you got with FQL and then query DB for rows that exists in results to filter those present in DB from results...
$fql = <<<FQL
SELECT name, page_id FROM page WHERE page_id IN (
SELECT page_id FROM page_admin WHERE uid = me()
)
FQL;
// Get results from API
$pages = $facebook->api('/fql', array('q' => $fql));
// Collect pages for later usage by ID
$pagesByIds = array();
foreach($pages['data'] as $page){
$pagesByIds[$page['page_id']] = $page;
}
// Query DB for all pages that exists in results
$pagesIds = implode(',', array_keys($pageByIds));
$res = mysql_query("SELECT page_id FROM pages WHERE page_id IN ({$pageIds})");
while($pageRow = mysql_fetch_assoc($res)){
$pageId = $pageRow['page_id'];
// Remove pages that present in API results and DB
if (isset($pagesById[$pageId])) unset($pagesById[$pageId]); ;
}
// Display details for pages not existing in DB
foreach ($pagesByIds as $page){
echo "ID: {$page['page_id']}, Name: {$page['name']} \n"
}
Upvotes: 1
Reputation: 7438
<?php
$IDs = array();
foreach($PageNames['data'] as $PageName){
$IDs[] = $PageName['page_id'];
}
$investigate_id = mysql_query('SELECT page_id FROM pages WHERE page_id IN (\''.implode('\', \'', $IDs)).' LIMIT 1 ');
while($assoc = mysql_fetch_assoc($investigate_id)){
// iff
}
?>
Using WHERE col IN will make one query will all ID so
WHERE page_id IN (1,3,45,6,7,6,7,5,2,1,5,76) and it will do it in query, just make the loop after over the results and do whatever you want !
Upvotes: 2