PHP - Paging from an array

Soldato
Joined
1 Feb 2006
Posts
8,188
Hi guys, looked everywhere else for this so posting here as a last resort. I am looking to work out how to do paging in my php file. Basically I have an array of image names which are then used to dynamically create links to images. I want to limit the number of images per page.

Once my array is populated I iterate through it like...

Code:
foreach ($images as $file) {
    
   //$images being my array and $file being each filename
   //output links to images

}

How can I only output say 10 images on the first page, 10 on the second etc. I have done this before using mysql data but i cannot work it out for arrays.

Thanks in advance

How can
 
Code:
$per_page = 10;
$page = $_GET['page'] ? intval($_GET['page']) : 1;

$offset = $page * $per_page - $per_page;

if ( $offset > count($images) )
    die;

// show only the current page's images
for ( $i = $offset; $i < ($offset + $per_page); $i++ ) {
    if ( empty(@$images[$i]) )
        break;
    var_dump($images[$i]);
}

$total_pages = ceil(count($images) / $per_page);

// show links to all pages
for ( $i = 1; $i <= $total_pages; $i++ ) {
    echo "link to page $i";
}

completely untested and i just woke up so
 
just one issue coming out of the above...

I have been using a lightbox gallery and on each image there is a image counter i.e.
image 1 of 12.

Because I am using paging now the count only shows the contents of each page, in other words on each page the total goes from 1 to 16 rather than 1-16, 17-32 etc etc.

The count is taking from the number of img tags generated. I am guessing that there is no other way around this? A count is totally necessary to perform the paging so I cannot think of any other way to have a running total, rather than a 'per page' total.

Any ideas on this? Sorry if not explained well enough
 
Back
Top Bottom