SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: random image...

  1. #1
    SitePoint Enthusiast ErikN's Avatar
    Join Date
    Jul 2001
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    random image...

    Is there a way to use random() to display a random image from a directory? Because I want to setup this thing at my site so that a random image from a directory will be displayed.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure you just need to put the images from that directory into an array then select a random element from the array. Something like:


    PHP Code:
    <?
        
    function random_image($dir ".") {
            
    $d dir($dir);
            while(
    $f $d->read()) {
                if((
    $f != '.') && ($f != '..')) {
                    
    $images[] = $f;
                    }
                else {
                    continue;
                    }
                }
            
    $d->close();
            
    srand ((double) microtime() * 10000000);
            
    $randval rand(0,count($images) -1);
            return 
    $images[$randval];
            }

        print 
    random_image();    
            
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Enthusiast ErikN's Avatar
    Join Date
    Jul 2001
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, ok thanks.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by ErikN
    Hmmm, ok thanks.
    I suppose you would like to know how that works, right?

    All it does it load all the files from a given directory into an array then it picks one random file from that array and returns it from the function.

    PHP Code:
    //Prints one random filename from the current directory
    print random_image();  

    //Prints one random filename from a subdirectory named test of the current directory
    print random_image("test/");  

    //Assigns one random filename from the current directory to the variable $image
    $image random_image();  

    //Assigns one random filename from a subdirectory named test of the current directory to the variable $image
    $image random_image("test/"); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Enthusiast ErikN's Avatar
    Join Date
    Jul 2001
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, but i get the following errors when i try to do $image = random_image("/images/");

    Warning: OpenDir: No such file or directory (errno 2) in /home/edgenetwork/www.bandwnation.com/features/potd.php on line 3

    Fatal error: Call to a member function on a non-object in /home/edgenetwork/www.bandwnation.com/features/potd.php on line 4


    line 3 is $d = dir($dir);
    line 4 is while($f = $d->read()) {

    what went wrong?

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by ErikN
    thanks, but i get the following errors when i try to do $image = random_image("/images/");

    Warning: OpenDir: No such file or directory (errno 2) in /home/edgenetwork/www.bandwnation.com/features/potd.php on line 3

    Fatal error: Call to a member function on a non-object in /home/edgenetwork/www.bandwnation.com/features/potd.php on line 4


    line 3 is $d = dir($dir);
    line 4 is while($f = $d->read()) {

    what went wrong?
    Where is images/ located at? Is it in the features folder or at the root of your site? If its in the features folder, then you should have used $image = random_image("images/"); Notice the lack of the slash, if it was at the root of your site then you could use
    $image = random_image("../images/");
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Enthusiast ErikN's Avatar
    Join Date
    Jul 2001
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works, thanks!


Bookmarks

Posting Permissions

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