3

I have data I am receiving from a webservice call that I would like to display in a paged Drupal table. How can I do this? Can I use theme('table') and attach a pager to it somehow?

2 Answers 2

1

You need a generic array pager for paging data that isn't available in your Drupal database. There's a community documentation page available providing information about the subject.

From the page Paging non-SQL data:

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


/**
 * 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:


  $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));
0

Why not use Views to pull out the fields and use the table display? The pager is automatically built in.

4
  • How can I feed it to Views? We aren't saving any data into Drupal.
    – Kevin
    Commented Apr 6, 2012 at 19:06
  • oooh, I see, it is just coming straight from the feed to the page. How 'fresh' does the data have to be? Is it something that could be stored in the database? For example, create a content type that has fields that mimic the data that is coming in. As it comes in, save it programatically as a node of that content type, and then use Views to create exactly what you need to display it. There are also several modules that do this, the Feeds module for example - drupal.org/project/feeds.
    – blue928
    Commented Apr 7, 2012 at 1:13
  • It's too much data to store. Talking like 100k possible search results.
    – Kevin
    Commented Apr 9, 2012 at 1:15
  • Hmm the problem here is that, as you already know web pages are stateless, the data has to be stored 'somewhere.' Mysql could easily handle 100k records if you did want to go the database route. Otherwise, it would have to be stored in a PHP SESSION or there would be no way to save states between page changes. It just a HUGE array. You could just print that out 50 results at a time, or whatever. btw 100,000 records would be 1000 pages of 100 entries? I don't know the limitations of PHP, but theoretically the SESSION route is possible.
    – blue928
    Commented Apr 18, 2012 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.