Log in

View Full Version : PHP randomizer show file and filename



Feldt
07-24-2008, 06:30 PM
I'm looking for a script that will allow me to randomize and show both an image and its name (without the .gif, .jpg. etc.). I already know how to use a PHP randomizer to show/pull images from a single directory, but I was wondering if it was possible to also show its filename at the same time without extensions i.e.

(pretend) actual randomized images: image01.gif image02.gif image03.gif
and then right beneath in text or wherever else I wish to place it: image01, image02, image03

I've seen some alternatives using arrays i.e. mostly banner/advertisement rotations, however I have over 5000 images I wish to rotate and don't want to enter them in manually.

Thanks for the help!

Nile
07-24-2008, 09:41 PM
Use techietims solution all the way down, mines terrible
Here you go, explanation below:


<?php
$imagesNum = 8; //the amount of images.
$random = rand(1,$imagesNum);
$data = array('image','png'); //array('prefix','type')
$imageDisplay = $data[0].$random.chr(46).$data[1];
$imageName = substr($imageDisplay, 0, strpos($imageDisplay,$data[1])-1);
echo "<img src='$imageDisplay' /><br />$imageName";
?>

Ok, starting with the first line:

$imagesNum = 8; //the amount of images.
This is the amount of images you have in the file

$random = rand(1,$imagesNum);
Leave this line as it is.


$data = array('image','png'); //array('prefix','type')

The first section where it says image, is what the prefix is, so image1 to image $imagesNum.
And the second section is the type, so above you said gif, so change it to gif.
And rest:


$imageDisplay = $data[0].$random.chr(46).$data[1];
$imageName = substr($imageDisplay, 0, strpos($imageDisplay,$data[1])-1);
echo "<img src='$imageDisplay' /><br />$imageName";

Leave it alone. In the image element though, if your using standard HTML, take away the back slash.

I hope this helps.

techietim
07-24-2008, 11:18 PM
Hmm, that's quite hefty nile.

Give this a go:


<?php
$imgs = array_merge(glob('*.gif'), glob('*.jpg'), glob('*.png')); //GIF's, JPG's and PNG's
$key = array_rand($imgs);
echo '<img src="' . $imgs[$key] . '" /><br />' . substr($imgs[$key], 0, -4);

techietim
07-24-2008, 11:21 PM
whoops. here you go:


<?php
$imgs = array_merge(glob('*.gif'), glob('*.jpg'), glob('*.png')); //GIF's, JPG's and PNG's
$key = array_rand($imgs, 5);
foreach($key as $i)
echo '<img src="' . $imgs[$i] . '" /><br />' . substr($imgs[$i], 0, -4) . '<br />';

Feldt
07-24-2008, 11:24 PM
Thanks both (Nile and techietim) for the quick responses =) Both work and thanks for taking the time to write them out.

edit// thanks techietim for the edit =)

Whoops one last question: would it be possible to make it so that the images and text can be on separate lines, like in the above example I gave

i.e.
image, image, image, image, image
< br > name, name, name, name, name

techietim
07-24-2008, 11:41 PM
<?php
$imgs = array_merge(glob('*.gif'), glob('*.jpg'), glob('*.png')); //GIF's, JPG's and PNG's
$key = array_rand($imgs, 5);
$nImgs = array();
$nCaptions = array();
foreach($key as $i){
$nImgs[] = $imgs[$i];
$nCaptions[] = substr($imgs[$i], 0, -4);
}
echo '<table border="0">
<tr>';
foreach($nImgs as $i)
echo '<td style="text-align:center"><img src="' . $i . '" /></td>';
echo '</tr><tr>';
foreach($nCaptions as $i)
echo '<td style="text-align:center">'.$i.'</td>';
echo '</table>';