Re: Choose random image from dir and display it

From: Fox (fox_at_fxmahoney.com)
Date: 10/30/04

  • Next message: Mike Lepore: "Random Selection of String Value"
    Date: Sat, 30 Oct 2004 00:59:54 -0500
    
    

    kingofkolt wrote:
    > I have a directory of images, called "random". In it are the following
    > files:
    >
    > 1.gif
    > 2.gif
    > 3.gif
    > 4.gif
    >
    > I use this script to choose a random image and display it:
    >
    > $i = -2; // $i = -2 so that the directory files ("." and "..") won't be
    > counted.
    > if ( $dh = opendir( "images/random" ) ) {
    > while ( false !== ( $file = readdir( $dh ) ) ) {
    > $i++;
    > }
    > closedir( $dh );
    > $rand = rand( 1, $i );
    > $randimg = '<img src="images/random/' . $rand . '.gif" />';
    > }
    > print $randimg;
    >
    > This works just dandy. However, I figure there must be a more efficient way
    > to count the number of files in a directory. Is there a directory's
    > equivalent of a function like file()? Or is my script the only way to count
    > the files?
    >
    > Thanks.
    > - JP
    >
    >

    php 4.3 or greater:

    $imgGroup = glob("images/random/{*.jpg,*.gif,*.png}", GLOB_BRACE);

    echo "
            <img src = {$imgGroup[rand(0, count($imgGroup)-1)]} />
           ";

    Fox
    **************


  • Next message: Mike Lepore: "Random Selection of String Value"

    Relevant Pages

    • Re: Internet Explorer - Firefox
      ... Firefox, the problem is how publisher displayed it in IE. ... I imagine it is a problem with how the script is ... suppose to display a search box for flights..i am pretty sure that the ...
      (microsoft.public.publisher.webdesign)
    • Re: Display a block of text in Firefox & Safari
      ... in FireFox and Safari it appears as a narrow ... There seems to be a needless reliance on the global variable 'NumberOfQuestionsShown', I've suggested a different strategy below that should be easier to maintain - it uses a single class to hide/show questions so the script doesn't need to know how many questions there are nor do they need to be consecutively numbered. ... var questionNum; ... Instead of going through all questions, how about giving them all the same style, then just modify the style to change display from 'none' to ''. ...
      (comp.lang.javascript)
    • Re: Conditional formatting
      ... happens if you step through the script in Script Debugger? ... I'd like to dynamically place a button on my layout. ... I only want the "Show All" button to display when I'm ... calculation determining how the button is labelled and describing what ...
      (comp.databases.filemaker)
    • Re: Internet Explorer - Firefox
      ... the Pub 2003 coding engine produces the code. ... the problem is how publisher displayed it in IE. ... original script given to you from the source, ... suppose to display a search box for flights..i am pretty sure ...
      (microsoft.public.publisher.webdesign)
    • Re: Internet Explorer - Firefox
      ... the Pub 2003 coding engine produces the code. ... the problem is how publisher displayed it in IE. ... I imagine it is a problem with how the script ... suppose to display a search box for flights..i am pretty sure ...
      (microsoft.public.publisher.webdesign)