SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot arthaus's Avatar
    Join Date
    Jun 2002
    Location
    Pasadena, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random Images Oddities

    Hi all,

    I'm using PHP code to read my images directory. And create a random CSS background-image.

    The PHP code:
    PHP Code:
    <?php
    if ( $dir = @ opendir ('../includes/img/random_img' )) {
        while (( 
    $file =readdir ($dir )) !== false ) {
            
    $images [] = $file ;
            }
        
    closedir ($dir ); 
        }

    $image =$images [rand (0,count ($images ) - 1)];
    ?>
    The usage:
    Code:
    body {
      background-image: url(<? echo '../includes/img/random_img/' .$image .'' ?>);
      }
    Every so often I get a blank background...
    And the code is:
    Code:
    background-image: url(../includes/img/random_img/..);
      or
    background-image: url(../includes/img/random_img/.);
    How can I resolve this oddity?

    Thankx in advance,
    Arthur

  2. #2
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    .
    and
    ..
    Are two special directories, the first representing the current directory, the second, the parent directory.

    Simply adjust your directory gathering statement, (as shown further down in the PHP manual page on readdir, lucky find!):
    PHP Code:
    <?php
    if ( $dir = @ opendir ('../includes/img/random_img' )) {
       while (( 
    $file =readdir ($dir )) !== false ) {
          if (
    $file != '.' && $file != '..') { 
             
    $images [] = $file ;
          }
       
    closedir ($dir );
    }
    ?>
    My name is Steve, and I'm a super-villian.

  3. #3
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you might also like to check array_rand in the manual as an alternate way of pulling the random array value.
    teckis - that's news to me.


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
  •