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 ";
}
else {
echo "<a href=\"images.php?page=" . ($page - 1) . "\">Previous</a> ";
}
for ($l = 1; $l <= $PageCount; $l++) {
if ($l == $page)
echo " [$l] ";
else
echo " [<a href=\"images.php?page=$l\">$l</a>] ";
}
if ($page == $PageCount) {
echo " Next";
}
else {
echo " <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;
}