display multiple random images

Discussion in 'PHP' started by Lukas, Nov 23, 2006.

  1. #1
    I want to display 3 random images ( plural) on 1 page using PHP.

    how do you do that..?

    I have found many random image scripts, but never one that works to display three different random images on 1 page. which is an html extension.

    here is the code I have that is displaying the same, albeit random image 3 times

    _______________________________
    <?php
    // rotate images randomly but w/o dups on same page - format:
    // <img src='rotate.php?i=0'> - rotate image #0 - use 'i=1'
    // for second, etc
    // (c) 2004 David Pankhurst - use freely, but please leave in my credit
    $images=array( // list of files to rotate - add as needed
    "image1.gif",
    "image2.gif",
    "image3.gif",
    "image4.gif",
    "image5.gif"
    "image6.gif" );
    $total=count($images);
    $secondsFixed=20; // seconds to keep list the same
    $seedValue=(int)(time()/$secondsFixed);
    srand($seedValue);
    for ($i=0;$<=2;$i<$total;++$i) // shuffle list 'randomly'
    {
    $r=rand(0,$total-1);
    $temp =$images[$i];
    $images[$i]=$images[$r];
    $images[$r]=$temp;
    }
    $index=(int)($_GET['i']); // image index passed in
    $i=$index%$total; // make sure index always in bounds
    $file=$images[$i];
    header("Location: $file"); // and pass file reference back
    ?>
    ____________________
     
    Lukas, Nov 23, 2006 IP
  2. crazybjörn

    crazybjörn Peon

    Messages:
    270
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The problem with that script is that it outputs the file directly to the browser, what you should do however is write a script that only echo's the 3 image locations. And then put that where you link the images, <img src="HERE">
     
    crazybjörn, Nov 24, 2006 IP
  3. mistermix

    mistermix Active Member

    Messages:
    2,326
    Likes Received:
    85
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Ive been looking for the same type of script.

    What I have done instead is to use the same script three times. Just rename the script and use a different directory for the images to make sure the same images arent chosen.

    I dont know if this is the best solution but its the only one I have seen.

    I use this script http://alistapart.com/d/randomizer/rotate.txt
     
    mistermix, Nov 24, 2006 IP
  4. Nikolas

    Nikolas Well-Known Member

    Messages:
    1,022
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    150
    #4
    here is a tutorial that does this thing.

    You must be a registered user, but I guess you wont mind on that :) (registration is free anyway)
     
    Nikolas, Nov 24, 2006 IP