By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

Image gallery w/ pagination - No image on 1st page

P: 5
The script does most of what it's supposed to do, but I have what seems to be a simple problem that I can't figure out. When the page intitially loads the first page does not display images. The same things happens when the "1" link is selected and ofcourse when "previous" is clicked from page 2. Other than this everything is a-ok. I think I just may need another set of eyes to check it out.

Thanks.

And here's the code:

[PHP]
<?php

require_once ('includes/functions.php'); // Connect to the db.

// Number of records to show per page:
$display = 2;

// Determine how many pages there are:
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.

// Open the directory:
$handle = opendir('portraits/images/');

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

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

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

// Calculate the number of pages.

if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}

}// End of np else.

// Recieve file #.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}



if ($num_pages > 1) {

// 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="'.$_SERVER['PHP_SELF'].'?pg=' . $pg . '&s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a>';
}

// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?pg=' . $pg . '&s=' . ($start + $display) . '&np=' . $num_pages . '"> Next </a>';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?pg=' . $pg . '&s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
} else {
echo $i . ' ';
}
}
}


$dir = 'portraits/images'; // Define the directory to view.

// Open the directory:
$dp = opendir($dir);

while (false !== ($file = readdir($dp))) {
$files[] = $file;
}

$list = array_slice($files, $start, $display);
foreach ($list as $image){

// Split up the array into parts.

if ( (is_file ("$dir/$image")) && (substr($image, 10 != '.')) ) {

// Get the image's size in pixels.
$image_size = getimagesize("$dir/$image");

// Calculate the image's size in kilobytes.
$file_size = round( (filesize("$dir/$image")) / 1024) . "kb";

// Make thumbnails..
list($t_width, $t_height) = create_thumbnail($image_size[0], $image_size[1]);

// Print the information.
echo " <a href=\"javascript:create_window('$image', $image_size[0],$image_size[1])\"><img src=\"$dir/$image\" width=\"$t_width\" height=\"$t_height\" border=0/></a>";
} // End of the IF.
}

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

?>
[/PHP]
Jan 1 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.