Hey everybody.

I have taken the Hoverbox Gallery, added Lightbox v2 as well as the PHP script from Slim to make the gallery dynamic. All of that went fine.

The problem is, I only want to display 15 images at a time per page from what the PHP script gets from the directory. In some directories, I have 200-300 images so I want to add dynamic pagination to the script as well that will see how many images there are, how many i want to display per page and create a dynamic navigation accordingly. Using examples from other scripts I found online as well as my very basic and primitive knowledge of PHP, the script below is the final product.

The page is rendering without any errors, the dynamic navigation links are coming up accurately and functionally. The header, the dynamic page title, the CSS and Javascript links are all coming in fine, the only thing that is not working is the images. No errors are rendered, that section of PHP just seems to spit out no results HTML. I have tried endlessly to debug this and try new things, remove parts, add parts, etc. This project has consumed my time for the past week and now I want to give it to you guys. What's going wrong and how do I fix it?

Code:
<?php

$display = 15;

if (isset($_GET['type'])) {

    if (($_GET['type']) == 'polaroids') {
	$filmtype = 'Polaroids';
	$imgdir = "polaroids/fullsize";
	$thumbdir = "polaroids/thumbs";
	$urltype = 'polaroids';

    } elseif (($_GET['type']) == '35mm') {
	$filmtype = '35mm';
	$imgdir = "35mm/fullsize";
	$thumbdir = "35mm/thumbs";
	$urltype = '35mm';

    } elseif (($_GET['type']) == 'digital') {
	$filmtype = 'Digital';
	$imgdir = "digital/fullsize";
	$thumbdir = "digital/thumbs";
	$urltype = 'digital';

    } elseif (($_GET['type']) == 'portraits') {
	$filmtype = 'Self-Portraits';
	$imgdir = "portraits/fullsize";
	$thumbdir = "portraits/thumbs";
	$urltype = 'portraits';

    } else {
	die ("No film type was selected. Please go back to the photos page and choose a film type.");
    }
} else {
    die ("No film type was selected. Please go back to the photos page and choose a film type.");
}

if (isset($_GET['np'])) {
    
    $totalPages = $_GET['np'];

} else { 

    $handle = opendir($imgdir);
while (false !== ($file = readdir($handle))) {
   $files[] = $file;
}

                $totalPix = count($files); // Get number of files in directory.
            

closedir($handle);// Close the directory.
            

// Calculate the number of pages.
    if ($totalPix > $display) { // More than 1 page.
        $totalPages = ceil ($totalPix/$display);
    } else {
        $totalPages = 1;
    }

}

if (isset($_GET['s'])) {
    $start = $_GET['s'];

} else {
    $start = 0;
}

$x=$start;
$current_page = ($start/$display) + 1;

function createGallery($path, $thumbpath){
	if (($dir = opendir($path))){
		// loops through while the readdir function returns true.
		while ((false !== ($file = readdir($dir))) && ($x <= ($start+$display))) {
			// picks only valid filenames
			if ($file != "." && $file != ".." && file !=!is_dir($file)) {
                        $imgtitlea = substr($file, 0, strrpos($file, '.'));
			$imgtitle = str_replace("_"," ",$imgtitlea);
			// echos to the client a nice unordered list of images
			echo
			"<li>".
			"<a href=\"".$path."/".$file."\" rel=\"lightbox[page]\" title=\"".$imgtitle."\">
			<img src=\"".$thumbpath."/".$file."\" alt=\"".$imgtitle."\" width=\"40\" border=\"0\" />
			<img src=\"".$thumbpath."/".$file."\" alt=\"".$imgtitle."\" class=\"preview\" />
			</a>".
			"</li>";
			$x++;
			}
		}
	}
	echo "</ul>";
}

print('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">'."\r\n");
print('<html>'."\r\n".'<head>'."\r\n");
print('<title>'.$filmtype.': Page '.$current_page.'</title>'."\r\n");
print('<link href="styles.css" rel="stylesheet" type="text/css" />'."\r\n");
print('<!--[if IE]>'."\r\n");
print('<link rel="stylesheet" href="css/ie_fixes.css" type="text/css" media="screen, projection" />'."\r\n");
print('<![endif]-->'."\r\n");
print('<link href="css/hoverbox.css" rel="stylesheet" type="text/css" />'."\r\n");
print('<link href="css/lightbox.css" rel="stylesheet" type="text/css" />'."\r\n");
print('<script type="text/javascript" src="js/prototype.js"></script>'."\r\n");
print('<script type="text/javascript" src="js/scriptaculous.js?load=effects"></script>'."\r\n");
print('<script type="text/javascript" src="js/lightbox.js"></script>'."\r\n");
print('<link href="../../experime.css" rel="stylesheet" type="text/css" />'."\r\n");
print('</head>'."\r\n".'<body>'."\r\n");


echo "<h1>".$filmtype.": page ".$current_page."</h1>";
echo "<ul class=\"hoverbox\">";

createGallery($imgdir, $thumbdir);
$end = $x;

if ($totalPages > 1) {
    
    echo '<br /><p>';
    // Determine what page the script is on.    
    $current_page = ($start/$display) + 1;
    
    // If it's not the first page, make a Previous button.
    if ($current_page != 1) {
        echo '<a href="index.php?type=' . $urltype. '&s=' . ($end - $display) . '&np=' . $totalPages . '">Previous</a> ';
    }
    
    // Make all the numbered pages.
    for ($i = 1; $i <= $totalPages; $i++) {
        if ($i != $current_page) {
            echo '<a href="index.php?type=' . $urltype. '&s=' . (($display * ($i - 1))) . '&np=' . $totalPages . '">' . $i . '</a> ';
        } else {
            echo $i . ' ';
        }
    }
    
    // If it's not the last page, make a Next button.
    if ($current_page != $totalPages) {
        echo '<a href="index.php?type=' . $urltype. '&s=' . ($end + $display) . '&np=' . $totalPages . '">Next</a>';
    }
    
    echo '</p>';
    
}

?>