I created this function... I don't know if it would be useful in enough cases to be added, but it's basically pager_query, except instead of getting a database query, it works directly on an array.

function pager_array($array, $limit = 10, $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);
}

I used it to make a listing of images from a directory, because I had a lot of images, and wasn't pleased with the current bulk upload implementations... Should this be in snippets instead? Feel free to repost it and close if so.

This could possibly be implemented in the pager_query function itself, testing whether it was an array or a string.

Comments

coreb’s picture

Version: x.y.z » 6.x-dev

Moving out of the "x.y.z" queue to a real queue.

psdopsdo’s picture

Category: feature » support

Hi Bradlis7,

I'm new in Drupal and hope to use the pager_array() function for displaying a great array.
Coul'd you give me some instructions ?

<1> I suppose i have to copy/paste it pager.inc ?

<2> Then how to use it ?.
My array is, for example, myArray[$i];

Thank you very much

bradlis7’s picture

Well, here's an example. I read an directory of images, and used the array like so:

$files = pager_array($files); // 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.
foreach($files as $f)
  echo "<img src='".url("$_GET[il_d]/$f")."' alt='' /><br />\n";
echo theme('pager'); // prints out the indexes for browsing to the next page and everything.
bradlis7’s picture

Category: support » feature
psdopsdo’s picture

First of all thank you for answering.
I do have fogetten something !

Drupal displays the first 5 items ( 5 instead of 10 for tests) and the pager displays "1 2 3 4 Next Last".
But :
. I have 93 items (and not 4*5=20). So i am waiting for "1...19 Next Last" ans not"1 2 3 4 Next Last"
. If I try $limit=10, there will be no pager
. The activated links is always "1" and i am never be able to activate the others links ("2", Last, Next). So only the first page of the array is displayed.
. The URLs under seems curious : http://www.mysite.com/?q=mymodule/arg&from=10 => $myArray is nowhere in the link ?

Thank you a lot for your help.

My array is : myArray[$i]= html string and $i is numeric
$pager_total is correct. The eval ou the number of page is correct too.

bradlis7’s picture

Hm, I'm really not sure. I had some problems getting the variables in the url to pass it seems. I don't use the function right now, but I did have .htaccess hacked to work.

You'll probably have to look at the generated source. I'm not really sure what's going on from what you said. You could put the code you used for pager_array (with an actual example array) and then html source generated from that.

Are you trying to use images?

psdopsdo’s picture

I have no image for the moment.
I follow your advice and look at the generated source.
Thank you for all.

pasqualle’s picture

Version: 6.x-dev » 7.x-dev
caspercash’s picture

thank you very much bradlis7! Indeed a very great help! thanks!

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.