Page 1 of 1
Pagination from array with files and tables :s
Posted: Mon Aug 14, 2006 1:17 pm
by jayshields
Code: Select all
$picsperrow = 4; //Amount of images to put on each row in the table
$rowsperpage = 4; //Amount of rows to show on each page
$resize2width = 200; //The width to resize all the images to (if GD is available)
if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] >= 1) {
$page = $_GET['page'];
} else {
$page = 1;
}
//Put all files into an array
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "Thumbs.db" && strtoupper(substr($file, -3)) == 'JPG') {
$files[] = $file;
}
}
closedir($handle);
}
natsort($files);
$x = 0;
foreach($files as $value) {
if(
$x >= $page-1*($picsperrow*$rowsperpage) &&
$x <= $page*(($picsperrow*$rowsperpage)-1)
) {
if($x % $picsperrow == 0) echo '<tr>';
if(function_exists('imagecreatefromjpg') && !is_file($dir . '/sml_' . $value))
image_resize_by_width($dir.'/'.$value, $dir.'/sml_'.$value, $resize2width) or die('Couldn\'t resize image.');
if(is_file($dir . '/sml_' . $value)) {
echo '<td align="center"><a href="'.$dir.'/'.$value.'"><img src="'.$dir.'/sml_'.$value.'" border="0" width="200" /></a></td>';
} else {
echo '<td align="center"><a href="' .$dir.'/'.$value.'"><img src="'.$dir.'/'.$value.'" border="0" width="200" /></a></td>';
}
if($x % $picsperrow == $picsperrow - 1) echo '</tr>';
}
$x++;
}
The maths for the pagination is really hurting my brain now, I've been looking at it for about an hour.
The first page shows fine.
The second page shows almost twice as many photos, starting with the very first one, and finishing one short of the one the second page should finish with.
I'd show you the page but it's got peronsal photos on and will hammer my server.
I'm almost sure the problem lies in the IF statement on the line after the foreach loop is started.
Any help is much appreciated

Posted: Mon Aug 14, 2006 1:27 pm
by s.dot
Well I think what you'll want to do is load the file names into an array. Then..
Code: Select all
$start = $current_page*$pictures_per_page+1;
$end = $start+$picture_per_page+1;
for($i=$start;$i<$end;$i++)
{
echo $picture_array[$i];
}
Posted: Mon Aug 14, 2006 1:58 pm
by jayshields
Thanks, I was thinking about changing the loop type but I thought I'd stick to my guns, obviously a bad idea.
I took your idea and played around with it and it's working OK now.
Thanks

Posted: Mon Aug 14, 2006 3:28 pm
by Jenk
for pagination you want the maths of:
Code: Select all
$start = $page_number * ($pics_per_page - 1);
$end = $page_number * $pics_per_page;
this may be of interest to you (*ahem* pimp)
Posted: Mon Aug 14, 2006 5:00 pm
by bokehman
Jenk wrote:for pagination you want the maths of:
Code: Select all
$start = $page_number * ($pics_per_page - 1);
$end = $page_number * $pics_per_page;
That's wrong!
Code: Select all
$end = ($page_number * $pics_per_page) - 1; // arrays start at zero
$start = $end - $pics_per_page;
Posted: Mon Aug 14, 2006 5:37 pm
by jayshields
I ended up with this:
Code: Select all
$start = ($page-1)*($rowsperpage*$picsperrow);
$end = $start+($rowsperpage*$picsperrow);
Posted: Mon Aug 14, 2006 7:05 pm
by Jenk
bokehman wrote:Jenk wrote:for pagination you want the maths of:
Code: Select all
$start = $page_number * ($pics_per_page - 1);
$end = $page_number * $pics_per_page;
That's wrong!
Code: Select all
$end = ($page_number * $pics_per_page) - 1; // arrays start at zero
$start = $end - $pics_per_page;
apologies, it is wrong. Corrected:
Code: Select all
$start = $pics_per_page * ($page -1);
$end = $pics_per_page * $page;
Posted: Tue Aug 15, 2006 2:39 am
by bokehman
Jenk wrote:apologies, it is wrong. Corrected:
Code: Select all
$start = $pics_per_page * ($page -1);
$end = $pics_per_page * $page;
That's still wrong. $start is ok now... but $end for page one now has the same value as $start for page 2