php

Soldato
Joined
24 Aug 2006
Posts
6,241
i can't get my head around this, being retarded. :confused:
say i want to have more than 2 pics per page on my pagination, how do i alter the variables below to slice the array for 10 per page?

Code:
//check to see if the page has been set
$page = (isset($_GET['page'])) ? (int)preg_replace('#[^0-9]#i','',$_GET['page']): 1;



$quantity = 2; //number per page

$list = scandir('image_gallery');
$list = array_slice($list, 2);

//Get the number of pages
$pages = ceil(count($list)/$quantity);

if ($page==1)
{
	$files_to_display = array_slice($list, 0, $quantity);
}
else if ($page==2)
{
	$files_to_display = array_slice($list, 2, $quantity);	
}
else if ($page>2)
{
	$files_to_display = array_slice($list, $page+$page-2, $quantity);	
}

?>
<h1>Photo Gallery</h1>
<br />
<?php echo 'page:' . $page ?>
<?php



foreach($files_to_display as $file)
{
    echo "<div class='file'><div class='image'><img src='image_gallery/$file'></div>$file</div>";
}

for ($i=1; $i <= $pages; $i++ )
{
	
	echo "<a href='?page=$i'>$i</a> ";
}

?>
<br />
 
but what about
$files_to_display = array_slice($list, 0, $quantity);
what should i put instead of '0' as the offset for the array slice?
page 1, should be 0
page 2, 10
page 3, 21
page 4, 31
 
Replace this monstrosity:
Code:
if ($page==1)
{
	$files_to_display = array_slice($list, 0, $quantity);
}
else if ($page==2)
{
	$files_to_display = array_slice($list, 2, $quantity);	
}
else if ($page>2)
{
	$files_to_display = array_slice($list, $page+$page-2, $quantity);	
}

With this:
Code:
$files_to_display = array_slice($list, ($page-1)*$quantity, $quantity);
 
thanks dude :)
So obvious

With this:
Code:
$files_to_display = array_slice($list, ($page-1)*$quantity, $quantity);
[/QUOTE]
 
Back
Top Bottom