Jump to content

Looping Problem with PHP Image Gallery


blueberrylolli

Recommended Posts

I found this tutorial for php gallery with pagination at (http://www.lateralcode.com/simple-php-gallery-pagination/).  It sorts the images according to time, so someone wrote a note about how to sort the files according to name here (http://www.lateralcode.com/reading-directories-in-sequence-in-php/).

 

I tried combining the two code with the following

 

	function getPictures() {
	global $page, $per_page, $has_previous, $has_next;
	if ( $handle = opendir(".") ) {
		$files = array();
		$lightbox = rand();
		echo '<ul id="pictures">';

		$count = 0;
		$skip = $page * $per_page;

		if ( $skip != 0 )
			$has_previous = true;

		while ( $count < $skip && ($file = readdir($handle)) !== false ) {
			$files[] = $file;
			}
		sort($files);
		foreach ( $files as $file ) {
			if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
				$count++;
		}
		$count = 0;
		while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
				$files[] = $file;
		}
		sort($files);
		foreach ( $files as $file ) {
			if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
				if ( ! is_dir('thumbs') ) {
					mkdir('thumbs');
				}
				if ( ! file_exists('thumbs/'.$file) ) {
					makeThumb( $file, $type );
				}
				echo '<li><a href="'.$file.'" rel="lightbox['.$lightbox.']">';
				echo '<img src="thumbs/'.$file.'" alt="" />';
				echo '</a></li>';
				$count++;
			}
		}
		echo '</ul>';

		while ( ($file = readdir($handle)) !== false ) {
			$files[] = $file;
			}
		sort($files);
		foreach ( $files as $file ) {
			if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
				$has_next = true;
				break;
			}
		}
	}
}

 

But now the pagination ($per_page, image thumbnails per page) doesnt' work.  It displays all the pictures in the folder, and when you click next page, it loops through the entire folder for images again. 

 

If I take out the sort code in this part

while ( $count < $skip && ($file = readdir($handle)) !== false ) {
			$files[] = $file;
			}
		sort($files);
		foreach ( $files as $file ) {
			if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
				$count++;
		}

 

The gallery still doesnt' show the correct number of images from $per_page, but on the second page, it won't loop through the entire folder.

 

If I take out the sort code in this section

while ( ($file = readdir($handle)) !== false ) {
			$files[] = $file;
			}
		sort($files);
		foreach ( $files as $file ) {
			if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
				$has_next = true;
				break;
			}
		}
	}

 

The "Next Page" button disappears and it's just one page with all the images from the folder, ignoring $per_page again.

 

Any help would be appreciated thanks so much!

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/173289-looping-problem-with-php-image-gallery/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.