Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Paging JSON results with PHP & AJAX

By: benjaminjkaiser on Mar 24th, 2013  |  syntax: PHP  |  size: 1.43 KB  |  views: 86  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.     $page_num = 1;
  3.     if(isset($_GET['page']){
  4.         $page_num = urlencode($_GET['page']);
  5.     }  
  6.     $id = urlencode($_GET['id']);
  7.     $url = "https://gdata.youtube.com/feeds/api/playlists/$id?alt=jsonc&v=2&start-index={$page_num}
  8. &max-results=50";
  9.     $content = file_get_contents($url);
  10.     $json = json_decode($content, true);
  11.     echo "<div id=\"info\"><center><h1><b>{$json['data']['title']}</center></b></h1><br>{$json['data']['description']} there are {$json['data']['totalItems']} uploaded in this playlist.</center><p></div><div class=\"decoration\"></div>";
  12.     echo "<div id=\"center\">";
  13.     echo "<ul>";
  14.     $count = 0;
  15.     foreach ($json['data']['items'] as $items) {
  16.         ++$count;
  17.         echo "<li style=\"width: 300px;min-height: auto;border: none;display: inline-block;margin: 5px;padding:10px;background-color: rgba(17,0,52,0.14);border-radius: 5px;\"><a href=\"video.php?plid={$json['data']['id']}&vidid={$items['video']['id']}\"><font size=\"5\" style=\"font-weight:bold;\">{$items['video']['title']}</font></a><Br \>";
  18.         echo "<a href=\"video.php?plid={$json['data']['id']}&vidid={$items['video']['id']}\"><img style=\"width:300;height:auto;\" src=\"{$items['video']['thumbnail']['hqDefault']}\" title=\"{$items['video']['title']}\" id=\"ytThumb\"></img></a></li>";
  19.     }
  20. echo "</ul>";
  21. echo "</div>";
  22.  
  23. if($count == 50){
  24.         echo "<a href='thispage.php?id=$id&startindex={$page_num}'>Get More Results...</a>"
  25. }
  26. ?>