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
Comment #1
coreb commentedMoving out of the "x.y.z" queue to a real queue.
Comment #2
psdopsdo commentedHi 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
Comment #3
bradlis7 commentedWell, here's an example. I read an directory of images, and used the array like so:
Comment #4
bradlis7 commentedComment #5
psdopsdo commentedFirst 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.
Comment #6
bradlis7 commentedHm, 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?
Comment #7
psdopsdo commentedI have no image for the moment.
I follow your advice and look at the generated source.
Thank you for all.
Comment #8
pasqualleComment #9
caspercash commentedthank you very much bradlis7! Indeed a very great help! thanks!