Click to See Complete Forum and Search --> : Mini Gallery thingo


Vasilli
08-21-2003, 09:34 PM
Hi all

I have been trying to get this ting to work for ages, and i can't figure out why it is being so annoying :)

what i am trying to do is create a type of mini gallery that will display a few images on a page. i have installed a copy of Albinator to a page i bulit befor this and from doing that i am compelled to create my own (just a much simpler and smaller one ;) ) But i have found that i have fallen into a hole and i can't crawl out.

in essence what i am trying to do is bulid an array of the images in a directory. ie loop through the directory and add the files to an array, then loop through the array and count the files, and then place them onto the page like so

Image 1 image 2 image 3
image 4 image 5 ......
and so on

Sounds simple (sorta) but that is what i got, it shoudl work, but doesn't, and i am sure i am overlooking something, prolly a real basic thing, but i am stumped.

any help would be great, thanks




<?php
$folder = 'images/';

if (is_dir($folder)){

$vOpenedDir = opendir($folder);
echo "\n\n<table bgcolor=\"#ffcc00\" cellpadding=\"2\" cellspacing=\"2\" width=\"100%\">\n\t<tr>\n\t\t";


while (($file = readdir($vOpenedDir)) != false) {
$sDirItem = $folder.$file;

if ($file != "." && $file != "..");
{
$ct = array($file);

$iCounter = 0;

for ($iCounter = 0; $iCounter < count($ct) ; $iCounter++)
{
$ct[$iCounter] = ($iCounter + 1);

if ($file % 3 == 0)
{
echo "\t</tr>\n\t<tr>\t\t";
}
}

$sBuild = "\n\t\t";
$sBuild .= "<td align=\"center\" valign=\"middle\" style=\"border: 1px solid #666666\">";
$sBuild .= "\n\t\t";
$sBuild .= "<img src='". $folder . $file . "' width='90'><br>".$file ;
$sBuild .= "\n\t\t";
$sBuild .= "</td>\n";

echo $sBuild;

}
}
closedir($vOpenedDir);
echo "\t</tr>\n</table>";
}

pyro
08-22-2003, 08:05 AM
You're problem is probably the for loop inside the while loop. But, from looking at that code, it could be programmed a bit better. Take a look at this example:

<?PHP

$folder = "images/";
$handle = opendir($folder);

# Making an array containing the files in the current directory:
while ($file = readdir($handle))
{
$files[] = $file;
}
closedir($handle);

#loop through the files, and only take files that are images.
foreach ($files as $file) {
$f_name = $file;
$fileextention = split ('[.]', $f_name);
$fileext = $fileextention[count($fileextention)-1];
if($fileext=="gif" or $fileext=="png" or $fileext=="bmp" or $fileext=="jpg" or $fileext=="jpeg" or $fileext=="jpe" or $fileext=="tif" or $fileext=="tiff") {
$filename[] = $file;
}
}
#echo out the images...
echo "<table style=\"width: 100%;\">\n\t
\t<tr>\n\t\t<td>Images</td>";

for ($i = 0; $i < count($filename); $i++) {
if ($i % 3 == 0) {
echo "\n\t</tr>\n\t<tr>\n";
}
echo "\t\t<td style=\"width: 33%; border: gray 1px solid; padding: 3px;\"><img src=\"".$folder.$filename[$i]."\" alt=\"Gallery Image\"\"></img><td>\n";
}
echo"\n\t</tr>
</table>\n";
?>

Vasilli
08-22-2003, 07:32 PM
Pyro, Mate!!

I am sure that once before i have called you a PHP God, and yet again, you have proved yourself worthy of this title. I had tried so many things, and as ia m quite new to PHP, and coding in general, you have shoed me up (In a good way of course)

So thanks for your time and help

Maybe one day i will be able to return the favour (prolly not at PHP by the looks of it thoug)

Thanks Mate

Jono

pyro
08-22-2003, 08:03 PM
I'm glad I could help... :) PHP has got to be the most fun language out there... :D