Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Location
    USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Random Signature, is it possible?

    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?

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    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.
    Leonard Whistler

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Location
    USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool.

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

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by FirefoxWiz View Post
    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.
    Leonard Whistler

  • #5
    New to the CF scene
    Join Date
    Jul 2007
    Location
    USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Alright, I'll search for it.

    I've never coded anything before though..

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by FirefoxWiz View Post
    Alright, I'll search for it.

    I've never coded anything before though..
    This would be a very good starter project to learn PHP.
    Leonard Whistler

  • #7
    New to the CF scene
    Join Date
    Jul 2007
    Location
    USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I got it to list all of the images in the folder with this code
    PHP 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...
    Last edited by FirefoxWiz; 07-12-2007 at 12:08 AM. Reason: editing

  • #8
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by FirefoxWiz View Post
    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.
    Leonard Whistler


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •