1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Script for random image or flash file?

Discussion in 'Programming' started by Johnburk, Nov 3, 2005.

  1. #1
    Is there a script (php) that shows a random image or flash file everytime a page is refreshed?
    SEMrush
     
    Johnburk, Nov 3, 2005 IP
    SEMrush
  2. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #2
    its easy enough to make one. Be more specific about what you need.
     
    mitchandre, Nov 3, 2005 IP
  3. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have a website and I would like visitors to see a new image or flash (both would be even better) everytime they visit my site, this could give my site a fresh look. A radom image of flash choosen from 6 or 7. PHP would be the best option
     
    Johnburk, Nov 3, 2005 IP
  4. FastBuffalo

    FastBuffalo Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I do this with ASP. I use a generic random function that randomly gives me a number between 1 and 7 and then I insert that random number in the image tag.

    Example image(3).jpg

    Name your images image1.jpg to image7.jpg

    Thats the way I do it. It should be easy to find a PHP random function.

    Hope this helps.
     
    FastBuffalo, Nov 3, 2005 IP
  5. FastBuffalo

    FastBuffalo Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    try this fuction and change the 100 to the number of images you have

    http://www.alt-php-faq.org/local/28/
     
    FastBuffalo, Nov 3, 2005 IP
  6. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    FastByffalo, Thank you for your fast realy. I just don't understand what you mean. I can't code myself and am looking for a existing script
     
    Johnburk, Nov 3, 2005 IP
  7. FastBuffalo

    FastBuffalo Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Don't have any off-hand, don't use PHP but hotscripts.com has always been a good source for me. I'm sure they have some prebuilt scripts.
     
    FastBuffalo, Nov 3, 2005 IP
  8. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Johnburk, Nov 3, 2005 IP
  9. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Those four don't seem to do the job (for free)

    
    <?php 
    
    
        $folder = '.'; 
    
    
        $extList = array(); 
        $extList['gif'] = 'image/gif'; 
        $extList['jpg'] = 'image/jpeg'; 
        $extList['jpeg'] = 'image/jpeg'; 
        $extList['png'] = 'image/png'; 
    
    
    $img = null; 
    
    
    if (substr($folder,-1) != '/') { 
        $folder = $folder.'/'; 
    } 
    
    
    if (isset($_GET['img'])) { 
        $imageInfo = pathinfo($_GET['img']); 
        if ( 
            isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) && 
            file_exists( $folder.$imageInfo['basename'] ) 
    ) { 
        $img = $folder.$imageInfo['basename']; 
    } 
    } else { 
        $fileList = array(); 
        $handle = opendir($folder); 
        while ( false !== ( $file = readdir($handle) ) ) { 
            $file_info = pathinfo($file); 
            if ( 
                isset( $extList[ strtolower( $file_info['extension'] ) ] ) 
    ) { 
                $fileList[] = $file; 
            } 
        } 
        closedir($handle); 
    
    
        if (count($fileList) > 0) { 
            $imageNumber = time() % count($fileList); 
            $img = $folder.$fileList[$imageNumber]; 
        } 
    } 
    if ($img!=null) { 
        $imageInfo = pathinfo($img); 
        $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ]; 
        header ($contentType); 
        readfile($img); 
    } else { 
        if ( function_exists('imagecreate') ) { 
            header ("Content-type: image/png"); 
            $im = @imagecreate (100, 100) 
                or die ("Cannot initialize new GD image stream"); 
            $background_color = imagecolorallocate ($im, 255, 255, 255); 
            $text_color = imagecolorallocate ($im, 0,0,0); 
            imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color); 
            imagepng ($im); 
            imagedestroy($im); 
        } 
    } 
    ?> 
    
    Code (markup):
    This one does, but how can it do the same with flash files?
     
    Johnburk, Nov 3, 2005 IP
  10. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Seems complicated for something so easy, how many images are we talking about? If you name the files like the above poster suggests image0, image1, image2. The code length would be 5 lines.
     
    mitchandre, Nov 3, 2005 IP
  11. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I was wrong its 4 lines :p

    
    <?php
    $random_integer = rand(0,10); // Assumes you have images from image0 to image10
    echo "<img src=\"http://www.domain.com/image", $random_integer, ".jpg\"></img>", ; //point to the correct domain/folder, this assumes .jpg but can be easily changed
    ?>
    
    Code (markup):
    If you use it, a link would be nice. :)
     
    mitchandre, Nov 3, 2005 IP
    Johnburk likes this.
  12. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #12
    @mitchandre


    Thank you, how can it be done for flash files?
     
    Johnburk, Nov 4, 2005 IP
  13. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #13
    so that it'll display either an image or a flash file?

    Can you post your html code for displaying a regular flash file for your website. Different people use different html code for displaying swf files.

    Without knowing specifically what you are using. It'll look like this
    
    <?php
    $random_integer = rand(1,2);
    if (is_int($random_integer / 2)) {
    $random_integer = rand(0,10); // Assumes you have images from image0 to image10
    echo '<img src="http://www.domain.com/image', $random_integer, '.jpg"></img>'; //point to the correct domain/folder, this assumes .jpg but can be easily changed
    }
    else {
    $random_integer = rand(0,10); // Assumes you have flashs from flash0 to flash10
    echo 'insert html to generate your flash file here'; // Don't forget to incorporate $random_integer for the flash file number.
    }
    ?>
    
    Code (markup):
     
    mitchandre, Nov 4, 2005 IP
  14. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Here is my code for flash

    
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
      codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,42,0"
      id="Main" width="269" height="367">
      <param name="movie" value="Main.swf">
      <param name="bgcolor" value="#FFFFFF">
      <param name="quality" value="high">
      <param name="allowscriptaccess" value="samedomain">
      <embed type="application/x-shockwave-flash"
       pluginspage="http://www.macromedia.com/go/getflashplayer"
       width="269" height="367"
       name="Main" src="Main.swf"
       bgcolor="#FFFFFF" quality="high"
       swLiveConnect="true" allowScriptAccess="samedomain"
      ></embed>
    </object>
    
    Code (markup):
    So once I have put the code in a php file, how do I put the php file into my html page?
    Thank you
     
    Johnburk, Nov 4, 2005 IP
  15. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Hopefully you have php installed on your server. :)

    To place php into an html file is easy copy/paste the code I gave you into the html file your editing. Thats some of the power of php.

    This might work, if not we'll try something more blunt.

    
    <?php
    $random_integer = rand(1,2);
    if (is_int($random_integer / 2)) {
    $random_integer = rand(0,10); // Assumes you have images from image0 to image10
    echo '<img src="http://www.domain.com/image', $random_integer, '.jpg"></img>'; //point to the correct domain/folder, this assumes .jpg but can be easily changed
    }
    else {
    $random_integer = rand(0,10); // Assumes you have flashs from flash0 to flash10
    echo '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
      codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,42,0"
      id="Main" width="269" height="367">
      <param name="movie" value="Main.swf">
      <param name="bgcolor" value="#FFFFFF">
      <param name="quality" value="high">
      <param name="allowscriptaccess" value="samedomain">
      <embed type="application/x-shockwave-flash"
       pluginspage="http://www.macromedia.com/go/getflashplayer"
       width="269" height="367"
       name="Main" src="flash'; //assumes you named the flash file as flash
    echo $random_integer ,'.swf"
       bgcolor="#FFFFFF" quality="high"
       swLiveConnect="true" allowScriptAccess="samedomain"
      ></embed>
    </object>';
    }
    ?>
    
    Code (markup):
     
    mitchandre, Nov 4, 2005 IP
  16. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Again thank you,

    When I use this code, the only thing I seen it

    
    
    '; } ?> 
    
    
    Code (markup):
     
    Johnburk, Nov 4, 2005 IP
  17. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Try this, and let me know what line if any you get errors for.
    
    <?php
    $random_integer = rand(1,2);
    if (is_int($random_integer / 2)) {
    $random_integer = rand(0,10); // Assumes you have images from image0 to image10
    echo "<img src=\"http://www.domain.com/image", $random_integer, ".jpg\"></img>"; //point to the correct domain/folder, this assumes .jpg but can be easily changed
    }
    else {
    $random_integer = rand(0,10); // Assumes you have flashs from flash0 to flash10
    echo "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"";
    echo "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,42,0\"";
    echo "id=\"Main\" width=\"269\" height=\"367\">";
    echo "<param name=\"movie\" value=\"flash", $random_integer, ".swf\">";
    echo "<param name=\"bgcolor\" value=\"#FFFFFF\">";
    echo "<param name=\"quality\" value=\"high\">";
    echo "<param name=\"allowscriptaccess\" value=\"samedomain\">";
    echo "<embed type=\"application/x-shockwave-flash\"";
    echo "pluginspage=\"http://www.macromedia.com/go/getflashplayer\"";
    echo "width=\"269\" height=\"367\"";
    echo "name=\"Main\" src=\"flash", $random_integer, ".swf\"";
    echo "bgcolor=\"#FFFFFF\" quality=\"high\"";
    echo "swLiveConnect=\"true\" allowScriptAccess=\"samedomain\"";
    echo "></embed>";
    echo "</object>";
    }
    ?>
    
    Code (markup):
     
    mitchandre, Nov 4, 2005 IP
  18. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Now all I get to see it
    
    "; //point to the correct domain/folder, this assumes .jpg but can be easily changed } else { $random_integer = rand(0,10); // Assumes you have flashs from flash0 to flash10 echo ""; echo ""; echo ""; echo ""; echo ""; echo "
    
    Code (markup):
     
    Johnburk, Nov 4, 2005 IP
  19. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #19
    I think I got it working with this code, but this without image file and it cannot find the .swf file :(
    
    <?php
    $random_integer = rand(0,10); // Assumes you have flashs from flash0 to flash10
    echo> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
      codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,42,0"
      id="Main" width="269" height="367">
      <param name="movie" value="$random_integer".swf">
      <param name="bgcolor" value="#FFFFFF">
      <param name="quality" value="high">
      <param name="allowscriptaccess" value="samedomain">
      <embed type="application/x-shockwave-flash"
       pluginspage="http://www.macromedia.com/go/getflashplayer"
       width="269" height="367"
       name="Main" src="flash'; //assumes you named the flash file as flash
    echo $random_integer ,'.swf"
       bgcolor="#FFFFFF" quality="high"
       swLiveConnect="true" allowScriptAccess="samedomain"
      ></embed>
    </object>
    Code (markup):
     
    Johnburk, Nov 4, 2005 IP
  20. FastBuffalo

    FastBuffalo Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    I agree, there should be a built in PHP function. (1 line)
     
    FastBuffalo, Nov 4, 2005 IP