Last updated July 13, 2012. Created by hatuhay on February 16, 2012.
Edited by Alan D.. Log in to edit this page.

Generic Array Pager

With the following function, you can use the Drupal core pager with any array with only two lines of code.

<?php
/**
* An generic array pager for Drupal.
* For Drupal 5 and 6, the default limit is 10. For Drupal 7 it is 9.
*/
function pager_array_splice($data, $limit = 9, $element = 0) {
  global
$pager_page_array, $pager_total, $pager_total_items;
 
$page = isset($_GET['page']) ? $_GET['page'] : '';
 
// Convert comma-separated $page to an array, used by other functions.
 
$pager_page_array = explode(',', $page);
 
// We calculate the total of pages as ceil(items / limit).
 
$pager_total_items[$element] = count($data);
 
$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_slice($data, $pager_page_array[$element] * $limit, $limit, TRUE);
}
?>

The usage is as simple as:

<?php
  $output
= '';
 
$tree = taxonomy_get_tree($vocab->vid);
 
$tree = pager_array_splice($tree, 5);
 
// Do something with the 5 terms
 
$output .= theme('pager', array('quantity' => 5));
?>

Similar SOAP example (D7)

We will define two templates to format data on our listings: the page and the item.

The page is themed thru function because no format is going to be applied as listing format will be done thru Drupal item_list.

Each item will be formatted thru template item to allow themers to give format on a html template.

<?php
/**
* Implements hook_theme().
*/
function page_soap_theme() {
 
$theme_hooks = array(
   
'page_soap_item' => array(
     
'template' => 'page_cine_item',
     
'variables' => array('image_raw' => NULL, 'title_raw' => NULL, 'trailer' => NULL, 'year' => NULL, 'teaser' => NULL, 'Id' => NULL,)
    ),
   
'page_soap' => array(
     
'render element' => 'element',
    ),
  );
  return
$theme_hooks;
}
?>

This is the funcion that gets XML data.

We use a function from http://php.net/manual/es/book.simplexml.php to transform XML data into an array.

Data is saved on cache for performance.

The pager is initialize thru pager_default_initialize() function.

Data is formatted using custom theme page_soap and pager is added using Drupal standard theme pager.

<?php
function page_soap_arguments() {
 
// Seeks for information in cache
 
if ($cache = cache_get('listing')) {
   
$sx = $cache->data;
  }
  else {
   
// If not in cache, gets external data
   
$sx = simpleXMLToArray(simplexml_load_file('http://www.soaporigin.com'));
   
// Saves information in cache for 6 hours: 60x60x6
   
cache_set('listing', $sx, 'cache', time() + 21600);
  }
 
// Gets number of records
 
$total = count($sx);
 
// Define items per page for paging
 
$num_per_page = 10;
 
// Initialize pager and gets current page
 
$page = pager_default_initialize($total, $num_per_page);
 
// Gets first record and last record to show
 
$offset = ($page) * $num_per_page;
 
$last = ($offset + $num_per_page < $total)? $offset + $num_per_page : $total;
 
// Calls page_soap theme
 
$render_array = theme('page_soap', array('array_raw' => $sx, 'offset' => $offset, 'last' => $last));
 
// Calls Drupal standard pager theme and set 5 page links on pager
 
$render_array .= theme('pager', array('quantity' => 5));
  return
$render_array;
}
?>

In the template preprocess we manipulate raw variables to prepare them for the theme file page_soap_item.tpl.php.
We are themming the image with theme_image() and the link wih l() function
This gives themer flexibility on the output of each line.
<?php
/**
* Preprocess the page_soap_item theme hook.
* Clear format for image and title, rest of variables stays the same.
*
* Inputs: $image_raw, $title_raw, $trailer, $year, $teaser, $Id.
*
* @see page_soap_item.tpl.php
*/
function template_preprocess_page_soap_item(&$variables) {
 
$variables['title'] = utf8_decode($variables['title_raw']);
 
$variables['image'] = theme('image',
    array(
'path' => "http://www.source.net/movies/" . $variables['image_raw'],
   
'alt' => $variables['title']));
 
$variables['link'] = l("Trailer",$trailer);
}
?>

Theming in this case is done thru a function, because this theme is not themable. The theme just gives format to each line and format them with the standard Drupal theme item_list. Each line has been formated using page_soap_item.tpl.php.
<?php
/**
* Preprocess the page_soap theme hook.
*
* Inputs: $array_raw.
*
*
*/
function theme_page_soap(&$variables) {
 
// Creates an array to pass to theme item by item
 
$items = $variables['array_raw'];
  for(
$i=$variables['offset'];$i<$variables['last'];$i++){
 
$items[] = theme('page_cine_item', array(
   
'image_raw' => $items[$i]['Photo'],
   
'title_raw' => $items[$i]['Title'],
   
'trailer' => $items[$i]['Trailer'],
   
'year' => $items[$i]['Year'],
   
'teaser' => $items[$i]['Teaser'],
   
'Id' => $items[$i]['IdShow'],
  ));
  }
 
// Calls drupal item_list
 
$render_array = theme('item_list', array('items' => $items, 'title' => t('Listig')));
  return
$render_array;
}
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Thanks for this post, it helps a lot. I just wanted to point out that after trying the function you pointed to on php.net for parsing the XML, it does not work with attributes very well in the form of <tag id="something">value</tag> and will throw an error when there is just a flat value within a tag with attributes. I am using this instead which deals with attributes correctly.

$sx = @json_decode(@json_encode(simplexml_load_string('http://www.soaporigin.com')),1); and I didn't test it but I'll bet much quicker.