Elena Freedom
Elena Freedom

Reputation: 13

Use facebook pagination to show the photos of a user

Hi i am trying to show the photos of a user in my application but i don't want them all too appear in the same page as they might be a lot and it causes delay. I haven't find any good examples to help me manage it. This is my code: It currently only presents the first 25 photos and the previous and next buttons are broken

if ($user) {

  try 
 {
$curPage = $_REQUEST["page"];
$curOffset = $curPage * 25;
$nextPage = $curPage + 1;
if ($curPage > 0) {
  $prevPage = $curPage - 1;
} else {
  $prevPage = 0;
} 
 $photos = $facebook->api('/me/photos/photos&offset='.$curOffset.'&limit=25');
 echo '<table><tr>';
foreach($photos['data'] as $photo)
{

 if($a <= 4){ //number of cells in row  print ('<a href="editor.php?photo_id='.$photo['id'].'"><img src='{$photo['picture']}'.'</a>'/>');

  echo '<td>';
  //echo "<img src='{$photo['picture']}' />";
  print ('<a href="editor.php?photo_id='.$photo['id'].'"><img src='.$photo['picture'].' /></a>');
  echo '</td>';
     $a++;
    }
 else {
  echo '</tr><tr>';
  echo '<td>';
    print ('<a href="editor.php?photo_id='.$photo['id'].'"><img src='.$photo['picture'].' /></a>');

  //echo "<img src='{$photo['picture']}' />";
  echo '</td>';
    $a = 1;
    }

}
echo '</tr></table>';
$canvas_base_url = "http://apps.facebook.com/piggypic/";
 echo '<center><a href="'.$canvas_base_url.'taggedphotos.php?page='.$prevPage.'"><-- prev page</a>';

echo '<a href="'.$canvas_base_url.'taggedphotos?page='.$nextPage.'">next page --></a></center>';

Any help will be really appreciated. Please guys I have been working on this for days...

Upvotes: 1

Views: 2397

Answers (1)

Lix
Lix

Reputation: 48006

You should do some experimentation with the Graph API Explorer, when you provide a limit and offset parameters to your calls to graph.facebook.com (as you have done above), you should get, in the response pagination links to the next and previous batch of items - check out this snippet of a result from the Graph API Explorer querying this endpoint :

https://graph.facebook.com/me/photos?limit=10&offset=15

     ...
  "paging": {
    "previous": "https://graph.facebook.com/me/photos?limit=1&format=json&since=1306425592&__previous=1", 
    "next": "https://graph.facebook.com/me/photos?limit=1&format=json&until=1306425591"
  }
}

As you can see the links are already there in the response from the previous call. Give it a try - its a great tool.

Upvotes: 2

Related Questions