Page 1 of 1

Thumbnail display and pagination/Script critique:

Posted: Thu Mar 03, 2005 4:19 pm
by mhulse
Hello.

I would love some feedback on the "thumbnail display" part of my gallery script... Any suggestions of ways to optimize the script (especially the part where I display the thumbnail images)...

If needed, I can post the config file and "larger image display" parts of my script.

This is the "thumbnail display" part of my gallery script:

Code: Select all

<?php

# Settings, look at config file for more info:
$biggy_display = $config&#1111;'big_path'];
$thumb_path = $_SERVER&#1111;'DOCUMENT_ROOT'].$config&#1111;'tn_path'];
$thumb_display = $config&#1111;'tn_path'];
$what_gal = $config&#1111;'gal_name'];

# Background color for your swf's:
$swfbkgrnd = '#9CA0AD';

# Put possible extensions into array "$files_of_type" for use in the "str_replace" function:
$files_of_type = array(".jpg", ".gif", ".png");

# Declare $thumbs and $biggy as arrays (Good practice):
$thumbs = array(); // Holds the thumbs.
$biggy = array(); // Holds the larger images.

$current_dir = opendir($thumb_path); // Look for picture files in directory and add them to array.
while ($file = readdir($current_dir)) &#123;
	# Check if it's a file, and it has a valid extension:
	if(eregi("(\.jpg|\.gif|\.png)$", $file)) &#123;
		$thumbs&#1111;] = $file;
		# Remove the "tn_" in name of thumbnails...
		# ...This will create a "large-image" array...
		# Large photos/folders are equal to thumbnail names without the tn_ at beginning of name...
		$biggy&#1111;] = str_replace("tn_", "", $file); 
	&#125;
&#125;
closedir($current_dir);

# Natural Sort of thumbnail images:
natcasesort($thumbs); // Does not seem to sort the images, but when print out array the text is?

if($thumbs == null) &#123;
	die("There are no files in this directory!"); // If no thumbnail images were found, alert user.
&#125; else &#123;
	# Count number of images in thumbnail array:
	$numElements = count($thumbs);
	for($counter=0; $counter < $numElements; $counter++) &#123;
		$big_folder = $biggy_display.str_replace($files_of_type, "", $biggy&#1111;$counter])."/"; // Var to hold possible path to folder.		
		$fldr = str_replace($files_of_type, "", $biggy&#1111;$counter]);
		if(is_dir($big_folder)) &#123;
			echo '<a href="'.$_SERVER&#1111;"PHP_SELF"].'?display='.$what_gal.'&test='.$fldr.'" target="_self"><img src="'.$thumb_display.$thumbs&#1111;$counter].'" alt="'.$thumbs&#1111;$counter].'" title="'.$fldr.'" /></a>'."\n";
		&#125; else &#123;
			echo '<a href="'.$_SERVER&#1111;"PHP_SELF"].'?display='.$what_gal.'&image='.$counter.'" target="_self"><img src="'.$thumb_display.$thumbs&#1111;$counter].'" alt="'.$thumbs&#1111;$counter].'" title="'.$fldr.'" /></a>'."\n";
		&#125;
	&#125;
&#125;

?>
Also, I am would love to paginate the thumbnails on the page... something really basic... for example:

<<prev ( [1][2][3] ) next>>

I have gotten the below script to display text and it will paginate the results, but I am not sure how to go about plugging it into my code:

Code: Select all

<?php

##### My attempt at pagination of thumbnails (Using the array $thumbs:

$thispage = $_SERVER&#1111;"PHP_SELF"].'?display='.$what_gal;
$num = count($thumbs); // number of items in list
$per_page = 2; // Number of items to show per page
$showeachside = 5; //**Number of items to show either side of selected page
if(empty($start)) &#123; $start=0; &#125; // Current start position
$max_pages = ceil($num / $per_page); // Number of pages
$cur = ceil($start / $per_page)+1; // Current page number

if(($start-$per_page) >= 0) &#123; 
	$next = $start-$per_page;
?>
<a href="<?php print("$thispage".($next>0?("&start=").$next:""));?>"><<</a> 
<?php
&#125;
print($cur);
print($max_pages);
print($num); 
if($start+$per_page<$num) &#123;
?>
<a href="<?php print("$thispage&start=".max(0,$start+$per_page));?>">>></a> 
<?php
&#125;

for($x=$start; $x<min($num,($start+$per_page)+1); $x++) print($thumbs&#1111;$x]."<br />");

?>
Any help/comments/suggestions/tips would rock!

Thanks in advance!
Cheers,
Micky

Posted: Thu Mar 03, 2005 4:28 pm
by John Cartwright
What exackly are you having trouble with?.. what's the problem?

Posted: Thu Mar 03, 2005 4:37 pm
by Chris Corbyn
I think he/she just wants to show his/her code and get some feedback.