Jump to content


Photo

Creating Gallery based on number of files in folder


  • Please log in to reply
1 reply to this topic

#1 robszol

robszol
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 25 April 2006 - 10:14 PM

OK, So let's say I have a folder named Gallery with files (1.jpg, 2.jpg, 3.jpg, 4.jpg etc...)

and I want to create a webpage where:

1). Creates a table with X row and 5 columns
2). The table's size is dependant on the files present in the folder "Gallery"
3). I want the pictures to be thumbnail size in the table
4). Each thumbnail links to original picture (1.jpg or 2.jpg etc.)
5). if step 4 is too tough then I could have a seperate folder called "
Thumbnails" with files (thumb1.jpg, thumb2.jpg etc...) linking to (1.jpg, 2.jpg etc) respectively.


so If I have 32 pictures in the Gallery folder then in the webpage, I'll have

32 thumbnail sized pictures in a table with 5 columns and 7 rows and the 7th row has 2 pics and the rest of row is blank.

How the bleep can I do this? I'm also new to PHP (but I know C++ to give u an idea of how much knowledge I have) and maybe PHP is too tough and perhaps there is a better way, but yeah I need your help!! !!

THANKS!!!!!!

#2 onepixel

onepixel
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 26 April 2006 - 10:08 PM

[!--quoteo(post=368648:date=Apr 25 2006, 05:14 PM:name=Robby)--][div class=\'quotetop\']QUOTE(Robby @ Apr 25 2006, 05:14 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
OK, So let's say I have a folder named Gallery with files (1.jpg, 2.jpg, 3.jpg, 4.jpg etc...)

and I want to create a webpage where:

1). Creates a table with X row and 5 columns
2). The table's size is dependant on the files present in the folder "Gallery"
3). I want the pictures to be thumbnail size in the table
4). Each thumbnail links to original picture (1.jpg or 2.jpg etc.)
5). if step 4 is too tough then I could have a seperate folder called "
Thumbnails" with files (thumb1.jpg, thumb2.jpg etc...) linking to (1.jpg, 2.jpg etc) respectively.
so If I have 32 pictures in the Gallery folder then in the webpage, I'll have

32 thumbnail sized pictures in a table with 5 columns and 7 rows and the 7th row has 2 pics and the rest of row is blank.

How the bleep can I do this? I'm also new to PHP (but I know C++ to give u an idea of how much knowledge I have) and maybe PHP is too tough and perhaps there is a better way, but yeah I need your help!! !!

THANKS!!!!!!
[/quote]



1- First read the folder containing your pictures to retrieve that full-size files name.
Verify sure the filename does not contain the string "thumb" before storing that file name in an array.
use the stripos ( $filename, 'thumb') function to make sure the returned value is FALSE


function GetPictures($dir){
$picturearray=array();

if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {

if(! stripos ( $file, 'thumb') ){$picturearray[]=$file; }

}//end while loop

closedir($dh);
}
}


return $picturearray;
}//end function


No need to have two folders for the thumbnails and the full-size pictures. Just create a filter that will allow your script to know the difference.

2- Once you have that array of full picture file name, pass it to a function that can loop through to create the table string.


function CreateTableString($filearray, $folderpath){
$n=count($filearray);

$Str='<table width="100%" cellpadding="5" cellspacing="0" border="0"><tr>';

foreach($filearray as $k=>$file){
if($k>0 && $k%4==0){$Str.='</tr><tr>'; }

$Str.='<td align="left" valign="top"><a href="'.$folderpath.'/'.$file.'">
<img src="'.$folderpath.'/thumb'.$file.'" width="80" height="80" border="0">
</a></td>';
}

$r=($n%5);

if($r>0){$Str.='<td colspan="'.$r.'"></td>';}

$Str='</tr></table>';

return $Str;
}



$Folderpath='path to the folder containing your pictures'; //basolute or relative to the current PHP script
$Picturearray=GetPictures($Folderpath);
$TableStr=CreateTableString($Picturearray, $Folderpath); // this is your thumbnails table string


Does not matter how many pictures you have. The only restriction is the correlation between the tumbnail and the full-size picture names.

I hope this will helps.

<a href="http://www.saheltech.com">PHP Programming - WordPress Blog Customization</a>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users