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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.