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 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