how to apply pager to an array in drupal

October 22nd, 2009 by Bruce Dou Views:156

add this function

function pager_array($array, $limit = 20, $element = 0) {
global $pager_page_array, $pager_total, $pager_total_items;
$page = isset($_GET['page']) ? $_GET['page'] : ”;

$pager_page_array = explode(‘,’, $page);

$pager_total_items[$element] = count($array);
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
$pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) – 1));
return array_splice($array,$pager_page_array[$element]*$limit,$limit);
}

how to use
// this was filled with all elements, and pager_array simply makes the array only have the elements that are to be displayed on the page.
$files = pager_array($files);
foreach($files as $f)
echo “
\n”;
echo theme(‘pager’); // prints out the indexes for browsing to the next page and everything.

some urls about ajax in drupal:

http://11heavens.com/using-ajax-in-Drupal-6

http://www.drupaleye.com/node/26

http://drupal.org/node/42562

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • 豆瓣
  • DZone
  • LinkedIn
  • MySpace
  • Reddit
  • RSS

Also see:

Tags: , ,

Leave a Reply