Click to See Complete Forum and Search --> : Photo Gallery code


fugazzi007
12-15-2004, 03:23 PM
I wanted a photo gallery where I could just throw all my images in two directories (image and thumbnails) and have php parse the filenames and display them with pagination using divs.

I finally got it all pretty and working, but I was wondering if there's a better way to get the dynamic data than what I am doing. It just seems a little sloppy.



$page = $HTTP_GET_VARS['page'];
$limit = 16; //limit of images per page
$thumbnail_dir = "thumbnails/";
$image_dir = "images/";

## Read Directory and assign all files to scalar ##
$mydir = dir('/public_html/thumbnails');
while(($file = $mydir->read()) !== false) {
if ($file !== "." && $file !== "..") {
$file_list .= "$file\t";
}
}
$mydir->close();

## Pass scalar to array and count number of files ##
$file_list = split("\t", $file_list);
$file_count = count($file_list)-1;
sort ($file_list);
array_shift($file_list);

## Parse array to create scalar limited by number per page ##
for ($i=0; $i<$file_count; $i++) {
$remainder = $i % $limit;
if ($remainder == 0 && $i !== 0) {
$split_file_list .= "\t\t";
}
$split_file_list .= "$file_list[$i]\n";
}

## Parse scalar to array and count number of pages ##
$Page = split ("\t\t", $split_file_list);
$PageCount = count($Page);



The following is the display code, so you have a better idea what the code is providing.



########## Display ###########
// Begin Pagination
echo "<div id=\"spacer\"><BR></div><div id=pagination>";
if ($page == 1) {
echo "Previous&nbsp";
}
else {
echo "<a href=\"images.php?page=" . ($page - 1) . "\">Previous</a>&nbsp";
}
for ($l = 1; $l <= $PageCount; $l++) {
if ($l == $page)
echo "&nbsp[$l]&nbsp";
else
echo "&nbsp[<a href=\"images.php?page=$l\">$l</a>]&nbsp";
}
if ($page == $PageCount) {
echo "&nbspNext";
}
else {
echo "&nbsp<a href=\"images.php?page=" . ($page + 1) . "\">Next</a>";
}
echo "</div>";
// End Pagination

## Get all files for each page ##
$filename = split ("\n",$Page[$page-1]);
## Get number of files per page (may be less than limit ##
$filename_count = count ($filename)-1;
echo "<div id=spacer><BR></div><div id=gallery>";
for ($m=0;$m<$filename_count;$m++) {
echo "<div id=float><a href= $image_dir$filename[$m]><IMG src=" . "$thumbnail_dir$filename[$m]></a><BR><p>$filename[$m]</p></div>";
}
echo "</div><div id=spacer><BR></div>";



And if you noticed, I used div's instead of tables, because they are supposed to be faster to load, so the following is the appropriate css bit.


div#navigation {
padding: 4px;
width: 800px;
border: #ffffcc 2px solid;
border-left: none;
border-right: none;
}
div#gallery {
width: 800px; //for the 600x800, but it's still a
// bit too large because of borders
}
// each image is a div, floated left. this means that
// every thumbnail has to be the same height.
div#float {
width: 25%;
float: left;
text-align: center;
}
// this is for the image name
div#float p {
text-align: center;
}
div#spacer {
clear: both;
}
// to match the main div
div#pagination {
width: 800px;
text-align: center;
}

planetsim
12-15-2004, 04:39 PM
Not bad however there are a few small things that i think should be done.


$mydir = dir('/public_html/thumbnails');
while(($file = $mydir->read()) !== false) {
if ($file !== "." && $file !== "..") {
$file_list .= "$file\t";
}
}
$mydir->close();

## Pass scalar to array and count number of files ##
$file_list = split("\t", $file_list);
$file_count = count($file_list)-1;
sort ($file_list);
array_shift($file_list);


Firstly $file_list is undefined so this will not work on most systems because of that. After that you split and return an Array, and seem to do so throughout. Why not just make it an Array.


$files = array();
$mydir = dir('/public_html/thumbnails');
while(($file = $mydir->read()) !== false) {
if ($file !== "." && $file !== "..") {
array_push($array,$file);
}
}


I dont see any need to sort it either, its usually sorted in Alphabetical Order however you should keep it in if you wish to be sure.


array_shift($file_list);

Any reason where getting rid of the first result completely and not sending it to a variable?

Other than that not bad havent had a chance to test it out.

fugazzi007
12-15-2004, 05:00 PM
Excellent! I knew there had to be something there to make it cleaner. The new code:



## Read Directory and assign all files to scalar ##
$file_list = array();
$mydir = dir('/public_html/thumbnails');
while(($file = $mydir->read()) !== false) {
if ($file !== "." && $file !== "..") {
$file_count = array_push($file_list,$file);
}
}
$mydir->close();

sort ($file_list);

## Parse array to create scalar limited by number per page ##
for ($i=0; $i<$file_count-1; $i++) {
$remainder = $i % $limit;
if ($remainder == 0 && $i !== 0) {
$split_file_list .= "\t\t";
}
$split_file_list .= "$file_list[$i]\n";
}

## Parse scalar to array and count number of pages ##
$Page = split ("\t\t", $split_file_list);
$PageCount = count($Page);



Wrt the sort, for some reason the script was returning a different order from my test to my live server. It may have been something to do with two different versions of php, or something else entirely; I'm still looking into it.

array_shift($file_list);

The reason for this is so the following for loop can start at 0, allowing for the correct remainder to be calculated. I'm not quite sure what you mean about sending it to a variable, or why.

Edit: Apparently writing directly to an array negated the need to shift the array. So the problem solved itself!

Thanks for looking!

fugazzi007
12-16-2004, 03:40 PM
Found out about array_chunk. I was using a for loop and modulous to create and array of arrays for my image pages. So, I basically cut out five lines of code and probably sped up the parsing.

Old Code

## Parse array to create scalar limited by number per page ##
for ($i=0; $i<$file_count-1; $i++) {
$remainder = $i % $limit;
if ($remainder == 0 && $i !== 0) {
$split_file_list .= "\t\t";
}
$split_file_list .= "$file_list[$i]\n";
}

## Parse scalar to array and count number of pages ##
$Page = split ("\t\t", $split_file_list);
$PageCount = count($Page);
$filename = split ("\n",$Page[$page-1]);
$filename_count = count ($filename)-1;


New Code

## Separate array into chunks of $limit ##
$Page = array_chunk($file_list,$limit);

## Count # pages, Set filename to specific page array, count number ##
## of images on page (so you only display images, no red x) ##
$PageCount = count($Page);
$filename = array();
$filename = $Page[$page-1];
$filename_count = count ($Page[$page-1]);


Edit: Turns out the php on my live server is older than the php on my test server, so I ended up adding the function to the live copy. So it is longer code. I still think it may be faster; I think I'll leave chunk in. Here's the link for the function. link (http://php.planetmirror.com/manual/en/function.array-chunk.php)