hellomello
hellomello

Reputation: 8597

facebook friendlist pagnation with foreach?

I'm retrieving facebook friendlist, but it retrieves like a billion photos (exaggerated). I was wondering how can I add pagination within a foreach loop? Also, would my PHP coding be the best way to retrieve the friendslist?

$user_friends = $facebook->api("/me/friends");

     foreach ($user_friends as $friends) {
      foreach ($friends as $friend) {
         // do something with the friend, but you only have id and name
         $id = $friend['id'];
         $name = $friend['name'];
          echo "<img src='https://graph.facebook.com/".$id."/picture' title='".$name."'>";
         echo $name;

      }

Would I need some kind of jquery to do the pagination? Thanks!

Upvotes: 1

Views: 1177

Answers (1)

Michael Pryor
Michael Pryor

Reputation: 25356

You can use the ORDER BY and the LIMIT keywords in an FQL query to make it so you only get a subset of the photos.

Something like (pseudocode)

$lastid = 0;
do 
{
 $photos = fql("SELECT id from photos where id > $lastid ORDER BY id LIMIT 100")
 foreach $photo in $photos
 {
  ... do stuff ...
  $lastid = $photo->id;
 }
} while ($photos.count > 0)

Basically you are limiting your query to 100 photo chunks and keeping track of where you are by ordering the IDs.

Upvotes: 2

Related Questions