...

View Full Version : Random Signature, is it possible?



FirefoxWiz
07-11-2007, 09:48 PM
I was wondering if this is possible, my brother tells me it isn't, but I'd like to know.

I know you can create a php script that way you can have a random signature image, like every time you refresh it changes to a different image.

However, when my brother did it, with his you have to add the new image filenames into the PHP script. I was wondering if it would be possible for the script to just grab a random image from a specific directory, instead of having to add the filenames in the script. He told me my idea wasn't possible, but I thought I'd ask.

Is it possible?

Len Whistler
07-11-2007, 10:13 PM
Yes...PHP can select all image files from a folder and display one at random without any changes to the PHP script whether there are 2 images or 2000+ images. PHP would read the directory and put all files into an array, then select one at random.

FirefoxWiz
07-11-2007, 10:16 PM
Cool.

Could you point me in the direction of how to do it please?

Len Whistler
07-11-2007, 10:21 PM
Cool.

Could you point me in the direction of how to do it please?

I don't know the correct syntax off hand but if you Google php read directory you will get some sample scripts you can work with. Also the online php manual has lots of sample scripts. Basically you will need to read the directory and put only image files into the array, then select one at random. Should be less than 10 lines of code.

FirefoxWiz
07-11-2007, 10:23 PM
Alright, I'll search for it. :)

I've never coded anything before though..

Len Whistler
07-11-2007, 10:25 PM
Alright, I'll search for it. :)

I've never coded anything before though..

This would be a very good starter project to learn PHP.

FirefoxWiz
07-11-2007, 10:59 PM
Ok, I got it to list all of the images in the folder with this code

<?php
if ($handle = opendir('images')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<img src='images/$file\n'>";
}
}
closedir($handle);
}
?>

But, I can't seem to make them come out as images.. and not text names. :S

EDIT
Ok, I got the images to show up, but how do I do it so a different one shows up on every refresh? Right now they're ALL showing up on the same page...

Len Whistler
07-12-2007, 02:50 AM
Ok, I got the images to show up, but how do I do it so a different one shows up on every refresh? Right now they're ALL showing up on the same page...

You are off to a very fast start. Before the echo statement you have to take the $file array and select a random file from it. The echo statement has to be moved outside of the loop. You will have to Google php array and check the php manual for some sample codes on organizing data from arrays.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum