Moved Friend's Website Now Photos Are Out of Order

I’m not a PHP coder. I can sometimes edit it around the edges to adapt a bit of code to a site I’m working on, but I don’t KNOW PHP. A friend needed to move his website here:

davidsmithstudio.com

From his old host to a new one and I helped him do it and everything works except after the move the photos on the php photo pages (lifestyle.php & etc) are out of order. The thumbnails work and expand to the correct and working lightbox popups, but for some reason the move changed the order and the changes are not uniform from page-to-page.

I found a workaround. I can look at the photo numbers in the file structure and their new placement on the pages and rename them and that appears to work (I have only renamed two files and they did in fact re-position to their original placement, but I’d like to know WHY this happened and a better, more correct, fix.

I asked the original site designer if there was a database that I needed to move and he said no, but looking at the PHP code I can’t see any obvious reference to order except when looking at the source code that’s being generated.

Can someone give me a suggestion on how to fix this?

Thanks!
Joel
Atlanta, GA

Check out this Article.

I’m guessing someone ordered the files a specific way…

Thanks for your response. I looked at the article and it’s likely beyond my skills to parse how his advice would apply. I’m sure someone did order them in a specific way, but I can’t find the code that did that. I’ve read through the javascript code that applies and all of the PHP files on the site, but can’t find a specific reference to order that might result in the images getting out of order as they are.

This is the code for the people.php page that appears to pertain to the images:

[php]


People and Portraits Portfolio

            <?php
	
	/* settings */
	$image_dir = 'images/portfolio/portrait/';
	$per_column = 8;
	
	
	/* step one:  read directory, make array of files */
	if ($handle = opendir($image_dir)) {
		while (false !== ($file = readdir($handle))) 
		{
			if ($file != '.' && $file != '..') 
			{
				if(strstr($file,'-thumb'))
				{
					$files[] = $file;
				}
			}
		}
		closedir($handle);
	}
	
	/* step two: loop through, format gallery */
	if(count($files))
	{
		foreach($files as $file)
		{
			$count++;
			echo '<a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
			if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
		}
	}
	else
	{
		echo '<p>There are no images in this gallery.</p>';
	}
	
?>
            
            
		</div>[/php]

This section:
/* step two: loop through, format gallery */

. . . appears to be the key to the order, but not knowing PHP I can’t see how it would result in a random result as has happened.

I can just rename the files, but I’d like to know why this happened so I can avoid it in the future.

Thank you for posting!

Joel
Atlanta, GA

BTW:

Could changing the PHP version (when moving to a new host) cause code irregularities that could result in the images not being ordered correctly?

Joel
Atlanta, GA

I don’t think so much that it’s a PHP Version, I think it would be more of the version/flavor of Linux you’re running that would be different, causing the order of the files to appear differently on your operating system.

Thanks for your responses. I found the solution:

[php] <?php

	/* settings */
	$image_dir = 'images/portfolio/industrial/';
	$per_column = 8;
	
	
	/* step one:  read directory, make array of files */
	if ($handle = opendir($image_dir)) {
		while (false !== ($file = readdir($handle))) 
		{
			if ($file != '.' && $file != '..') 
			{
				if(strstr($file,'-thumb'))
				{
					$files[] = $file;
				}
			}
		}
		closedir($handle);
	}
	
	natsort($files);
	
	/* step two: loop through, format gallery */
	if(count($files))
	{
		foreach($files as $file)
		{
			$count++;
			echo '<a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
			if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
		}
	}
	else
	{
		echo '<p>There are no images in this gallery.</p>';
	}
	
?>[/php]

I Googled “PHP image sorting” in various ways and found a number of articles and noticed one about “natural sorting” and the accompanying code. Then I found an online manual for PHP and looked up sorting options then I went back and looked in the original code for any sorting commands and saw none, so I decided that could be the answer and came up with:

	natsort($files);

I looked for a logical place to plug it in and it worked!

“Natsort” was the solution. The images immediately sorted by file number.

Joel
Atlanta, GA

BTW, I don’t know how the site’s creator sorted those same images on the old host. The site was moved intact and worked immediately, but it took a week or so to see that the photos were not in order, so whether there was some database or background code affecting the sort without the PHP code I added? I don’t know. I asked the site’s creator if there were any databases or exterior code that could affect the move and he indicated that there was not.

Joel
Atlanta, GA

Sponsor our Newsletter | Privacy Policy | Terms of Service