How to use theme_pager without a SQL query (and go to hell)

pager.inc is... ugly.  Let's get that out of the way.  I'm sure there are valiant souls out there who are not bitching about it, but trying to fix it.  I'm not one of those people right now, I've got my own battles, but if you happen to go up against it and want to create a pager for a data source which isn't a SQL database, here's what you need to do (mostly copied from pager_query):

Global variables (yes and it gets worse)

pager.inc makes extensive use of global variables to pass around the state of the pager.  You can see how these are defined in pager_query():

global $pager_page_array, $pager_total, $pager_total_items;

Honestly, don't ask me what these are.  Roughly, $pager_page_array is an array of different pagers on the page and what page they are at (I think).  $pager_total is the total number of pages for each pager, $pager_total_items is the number of items in the set.

These are all set in pager_query if you care to find out more.

 

Getting the current page

You'll need this of course to split your set up so you can show just what you need to show on that page.

Fortunately, this is pretty straightforward in a really obtuse way:

$page = isset($_GET['page']) ? $_GET['page'] : '';
// The user will go to mypage?page=0,3,5,6.
// What does this mean?  There are 4 pagers on the page.
// They don't have names, just numbers.
// Sorta like in a prison, only it's a mental facility and the inmates are running it.
// The first one is on page 0, the 2nd one on page 3, etc. etc

$pager_page_array = explode(',', $page);
// So this builds an array of all the pagers on the page and what page they are on.
// One of these is the pager you care about. In most cases, it is the only one, 
// so you can just set $element = 0;
$yourpage = $pager_page_array[$element];

Okay, now you do whatever you need to with that in terms of running a for loop to build a table or whatever.

Setting up the pager

// Number to show per page.
$limit = 10;
// The bizarre "element number" as long as you are the only pager on the page
// this will work fine
$element = 0;
// $total_records is a variable you define that reflects the total number of
// records you plan on showing (not per page but overall).
$pager_total_items[$element] = $total_records
// This is the total number of pages.  If you can't figure this out,
// go back to grammar school.
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
// wtf...
$pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], 
  ((int)$pager_total[$element]) - 1));
// No seriously... wtf... this is basically saying, I'm on the first page,
// unless I'm not. I know, it's really clear.

Let it loose

return $my_paged_content . theme('pager');
// Go get a drink.

Okay, I hope I helped someone.  Happy hacking.

Oh and don't forget to vote for my DrupalCon Copenhagen sessions!! I really want to go and got to get 'em accepted. If you want me to remind (harass) you when voting opens, please follow me on twitter @JacobSingh.

Tags: 

Comments

Thank you, that's very

Anonymous's picture

Thank you, that's very helpful.

This saved not only time

but also some PM stress to manage and finding out how it works.

One addition would be the theming of the correct elements. This is what I did

$page_num = $pager_page_array[$element];

$result = array();
foreach ($items as $index => $item) {
if ($page_num * $limit <= $index && $index < ($page_num+1) * $limit) {
$result[] = theme('search_result', $item);
}
}
$output = theme('item_list', $result);

The $yourpage can then be deleted right?

reply

Such projects investing resources for the youth to gain better access to education are certainly one of the best things that could have happened in the educational system. The more you invest in yourself, the more potential you`ll get to bring up to surface and the more effective you`ll be. I look back at the days I started my msn programs. I was so determined to occupy one day a management position in a public institution that I stood against any difficulty that crossed my way. It takes both passion and determination to dedicate your life to your most promising and constantly developing skills.

reply

Such projects investing resources for the youth to gain better access to education are certainly one of the best things that could have happened in the educational system. The more you invest in yourself, the more potential you`ll get to bring up to surface and the more effective you`ll be. I look back at the days I started my degrees at these <a href="http://www.cc-sd.edu/">San Diego colleges</a>. I was so determined to occupy one day a management position in a public institution that I stood against any difficulty that crossed my way. It takes both passion and determination to dedicate your life to your most promising and constantly developing skills.

I was very pleased to find

Network Camera's picture

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every Network Camera little bit of it and I have you bookmarked to check out new stuff you post.