How can this PHP/FQL code be modified to increase the performance and usability?

Posted by Kaoukkos on Stack Overflow See other posts from Stack Overflow or by Kaoukkos
Published on 2012-03-27T23:11:38Z Indexed on 2012/03/30 5:29 UTC
Read the original article Hit count: 238

Filed under:
|

I try to get some insights from the pages I am administrator on Facebook.
What my code does, it gets the IDs of the pages I want to work with through mySQL. I did not include that part though.
After this, I get the page_id, name and fan_count of each of those facebook IDs and are saved in fancounts[].

Using the IDs ( pages[] ) I get two messages max from each page. There may be no messages, there may be 1 or 2 messages max. Possibly I will increase it later. messages[] holds the messages of each page.

I have two problems with it.

  1. It has a very slow performance
  2. I can't find a way to echo the data like this:

ID - Name of the page - Fan Count
Here goes the first message
Here goes the second one

//here is a break

ID - Name of the page 2 - Fan Count
Here goes the first message of page 2
Here goes the second one of page 2

My questions are, how can the code be modified to increase performance and show the data as above? I read about fql.multiquery. Can it be used here?

Please provide me with code examples. Thank you

$pages = array(); // I get the IDs I want to work with
$pagesIds = implode(',', $pages);

// fancounts[] holds the page_id, name and fan_count of the Ids I work with

$fancounts = array();
$pagesFanCounts = $facebook->api("/fql", array(
  "q" => "SELECT page_id, name, fan_count FROM page WHERE page_id IN ({$pagesIds})"
));

foreach ($pagesFanCounts['data'] as $page){
  $fancounts[] = $page['page_id']."-".$page['name']."-".$page['fan_count'];
}


//messages[] holds from 0 to 2 messages from each of the above pages

$messages = array();
foreach( $pages as $id) {
  $getMessages = $facebook->api("/fql", array(
  "q" => "SELECT message FROM stream WHERE source_id = '$id' LIMIT 2"
  ));
  $messages[] = $getMessages['data'];
}

// this is how I print them now but it does not give me the best output. ( thanks goes to Mark for providing me this code )

$count = min(count($fancounts),count($messages));

for($i=0; $i<$count; ++$i) {
    echo $fancounts[$i],'<br>';
    foreach($messages[$i] as $msg) {
        echo $msg['message'],'<br>';
    }

}

© Stack Overflow or respective owner

Related posts about php

Related posts about facebook