|
Posted by Gleep on 03/04/07 20:12
On Fri, 02 Mar 2007 14:20:56 -0500, Fred Atkinson <fatkinson@mishmash.com> wrote:
> I need a PHP script that can be invoked refererencing a
>subdirectory where thumbnails are stored and have it randomly display
>one of those thumbnails.
>
> To expand upon my needs, here is the hierarchy of the Web
>site:
>
>main pages public_html
>
>subpages apple banana coconut
>
>sub-subpages aa bb cc dd ee ff gg
>
> Each of the aa-gg subdirectories will contain thumbnail .jpg
>files. I'd like to be able to invoke the script quite a few times
>within the same PHP page.
>
> Example:
>
> From the page in /banana, use <?php random_image $dir=aa> to
>return a thumbnail that it randomly took from sub page /banana/aa. I
>would invoke a thumbnail from another directory by changing aa to bb,
>cc, dd, ee, ff, or gg.
>
> So far, every script I've looked at I haven't quite been able
>to make work.
>
> Though I have written a few PHP scripts, I am a PHP novice and
>I need to 'keep it simple' so I can repeatedly use the script in the
>page.
>
> If the script is above the <html> tag and can be called from
>within the actual page, that should work fine for my purposes.
>
> Thanks for any help you can provide.
>
> Regards,
>
>
>
> Fred
THIS SHOULD WORK
<?
// set up the folder/subfolder arrays
$folder = array('apple', 'bannna', 'coconut');
$subfolder = array('aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg' );
// get all images into one array
$fileArray = array();
foreach($folder as $folderdir){
foreach($subfolder as $subfolderdir){
$dir = @opendir("./$folderdir/$subfolderdir");
while ($file = @readdir($dir)){
if( $file != "." AND $file != ".." ) {
if( strtolower(substr($file, -3)) == "jpg" )
$fileArray[] = "$dir/$file";
}
}
closedir($dir);
}
}
// randomize new array set - lets say you only need total 30 images to randomize
$random_images = array_rand($fileArray, 30);
// other alternative is to do a foreach for the entire array to get all the images
?>
<!-- a table to show all the images -->
<table width="500" border="0" cellspacing="4" cellpadding="0">
<tr>
<td><img src="<?= $fileArray[$random_images[0]] ?>" /></td>
<td><img src="<?= $fileArray[$random_images[1]] ?>" /></td>
<td><img src="<?= $fileArray[$random_images[2]] ?>" /></td>
<td><img src="<?= $fileArray[$random_images[3]] ?>" /></td>
<td><img src="<?= $fileArray[$random_images[4]] ?>" /></td>
<td><img src="<?= $fileArray[$random_images[5]] ?>" /></td>
<td><img src="<?= $fileArray[$random_images[6]] ?>" /></td>
</tr>
</table>
.... repeat all this till [$random_images[29]]
remember arrays start from 0 not 1
also you can force the images to be all one size by forcing the width heght
<img src="<?= $fileArray[$random_images[6]] ?>" width='xx' height='xx' />
[Back to original message]
|