Pagination from array with files and tables :s

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Pagination from array with files and tables :s

Post 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 :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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];
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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 :D
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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;
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I ended up with this:

Code: Select all

$start = ($page-1)*($rowsperpage*$picsperrow);
$end = $start+($rowsperpage*$picsperrow);
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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;
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

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