Could someone look at the code below and tell me why the pagination will not show up?
Thanks
Wiz
// Location of thumbnails directory
$thumb = "thumb/";
// Location of big images
$photos = "photos/";
// Number of columns to display (left to right)
$cols = 5;
// Number of images per page
$per_page = 25;
// Do not change anything below this line unless you know what your doing
if ($handle = opendir($thumb)) {
while (false !== ($image = readdir($handle))) {
if ($image != "." && $image != ".." && $image != rtrim($thumb,"/")) {
$files[] = $image;
}
}
closedir($handle);
}
$i = 0;
echo '<br /><center><table><tr>';
foreach($files as $file)
{
// Ignore Items the user does not have
if (empty($file))
continue;
$i++;
// Display Items the user has
echo '<td align="center"><a href="' . $photos . $file . '"><img src="' . $thumb . $file . '" /></a></td>';
if($i % $cols == 0)
echo '</tr><tr>';
if ($i == $per_page)
return;
}
echo '</tr></table></center>';
// Pagination
$total_pages = ceil(count($files) / $per_page);
// Get currently selected page, if not selected use 1 if too high use last page
if(isset($_GET['p'])){
$current_page = $_GET['p'];
if($current_page > $total_pages){
$current_page = $total_pages;
}
}
else
{
$current_page=1;
}
// do some math to determine starting array index to use for the current page
$start = $current_page * $per_page - $per_page;
print "File Listing for $dir<br>Page $current_page of $total_pages<br><br>";
// Print files for current page
for($i=$start;$i<$start + $per_page;$i++){
print "$files[$i]<br>";
}
// Print links for pages
for($j=0;$j<$total_pages;$j++){
$p = $j+1;
print "<a href='viewfiles.php?p=$p'>$p</a> | ";
}
$start = $current_page * $per_page - $per_page;I think that's it.
Not related, but a suggestion I've seen quite often, don't do this:
for($i=$start;$i<$start + $per_page;$i++){
print "$files[$i]<br>";
}
Do this:
for($i=$start, $len=$start + $per_page;$i<$len;$i++){
print "$files[$i]<br>";
}
To avoid recalculation on every iteration.