featured computer 850

PHP – Creating an HTML Thumbnail Table Code

Share page:

Note: This page is concerned with generating HTML when you already have your thumbnails and photos. I have another page if you want a script that can create thumbnails. Maybe deprecated with all the new ways to get this done, but here is an example of a working FOR loop in PHP.

Summary

This page describes a PHP script that can be used to create an HTML table of thumbnailed images. If you are not already comfortable with editing PHP code, then you probably need a more automated method. This is a code chunk you can put in your existing page with modifications. It’s a little dusty. You should also take out the styling and put it in a CSS file.

// Variables and math for files within target directory
$num_files = 0;
$num_rows = 0;
$remainder = 0;  
$i = 1;				//three counters
$j = 1;
$k = 1;  		

// php.ini parameter register_globals should be OFF.  
// Use superglobal _POST variable to access form data.

$alt_text = 				$_POST['alt_text'];
$current_path = 			$_POST['current_path'];
$current_text = 			$_POST['current_text'];
$final_directory = 			$_POST['final_directory'];
$final_path = 				$_POST['final_path'];

//////////////////////////////////////////////////////////////
// BEGINNING OF SECTION THAT CREATES A TABLE OF THUMBNAILS. 
// THUMBNAILS ARE IN A DIRECTORY INSIDE OF MAIN PHOTOS 
// DIRECTORY CALLED "thumbs" 
//////////////////////////////////////////////////////////////

// Fill an array with all items from a directory.
if ($handle = opendir($current_path)) {
	while (false !== ($file_name = readdir($handle))) {
	$pic_array[] = $file_name;
	//echo "$file_name<br>"; //debug
	}
closedir($handle);
}

// Take only the jpgs into a sorted array.
$n=1;
	//echo "<p>"; //debug:
foreach ($pic_array as $mypic){
	$file_ext = strstr($mypic,'.');
	if($file_ext == ".jpg" || $file_ext ==".JPG") {
	$nice_array[$n] = $mypic;
	//echo "nice_array $n is $nice_array[$n] <br>\n"; //debug
	$n = $n+1;
	}
}

// Get number of items in array and the remainder when divided by 3
$num_files = count($nice_array);
$num_rows = floor($num_files/3);
$remainder = $num_files%3;

// This is for debugging only.
// echo "\n<p>";
// echo "Number of files: $num_files<b>\n";

// echo "\n<p>";
// foreach ($pic_array as $mypic){
// echo "$mypic<br>";
// }


// Create a table of thumbnails 3 columns wide
echo "<table cellspacing=\"8\" cellpadding=\"8\" border=\"0\">\n\n";

	// Two FOR loops take care of the complete rows
	for ($i=1; $i<=$num_rows; $i++) {
		echo "<tr>\n";
			for ($j=1; $j<=3; $j++) {
				echo "<td align=\"center\"> <a href=\"$nice_array[$k]\"> <img src=\"thumbs/$nice_array[$k]\" width=\"192\" height=\"144\" border=\"0\" alt=\"$alt_text\"></a></td>\n";
				$k=$k+1;
			}
		echo "</tr>\n";
	}
	
// Use this logic to take care of remainder files
if ($remainder !== 0){
	if ($remainder == 2) {
		echo "<tr>\n";
		for ($j=1; $j<=2; $j++) {
			echo "<td align=\"center\"> <a href=\"$nice_array[$k]\"> <img src=\"thumbs/$nice_array[$k]\" width=\"192\" height=\"144\" border=\"0\" alt=\"$alt_text\"></a></td>\n";
			$k=$k+1;
		}
		echo "<td align=\"center\">*</td>\n";
		echo "</tr>\n";
	} elseif ($remainder == 1) {
		echo "<tr>\n"; 
		echo "<td align=\"center\"> <a href=\"$nice_array[$k]\"> <img src=\"thumbs/$nice_array[$k]\" width=\"192\" height=\"144\" border=\"0\" alt=\"$alt_text\"></a></td>\n";
		$k=$k+1;
		for ($j=1; $j<=2; $j++) {
			echo "<td align=\"center\">*</td>\n";
		}
		echo "</tr>\n\n";
	} else warn("Remainder value doesn't make sense.\n");
}			

echo "\n</table>\n\n";

Similar Posts