Re: Random Images with no duplicates?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This should work, although if there are only 9 lines in the file, it will
run infintely (or until the page times out)... so put a check in for that.

$imagesNeeded = 10;
$imageArr = array();
// Create array with $imagesNeeded number of elements.
for($i=0;$i<$imagesNeeded;$i++) {
  // choose image
  $tmpImg = $openFile[array_rand($openFile)];
  // Keep getting images until the image you have is not in the existing
array of images
  while(in_array($tmpImg,$imagesArr)) {
    // choose image
    $tmpImg = $openFile[array_rand($openFile)];
  }
  // Put unique image into array
  array_push($imageArr,$tmpImg);
}
// You now have an array called imageArr with 10 elements/images in it.

Aaron

On 12/15/05, Mike Rondeau <hyatt65@xxxxxxxxxxx> wrote:
>
> Hi,
>
> I'm still very green with PHP, but am working to fix that.
>
> I've searched high and low for an answer to this question and so far
> no solution has presented itself. I have a script to populate my web page
> with random images:
>
> <?php
> #random images example
> #this is your file
> $file = "images.txt";
> #open the file
> $openFile = file($file);
> #generate a random number
> srand((double)microtime()*1000000);
> #get one of the entries in the file
> $random_image = $openFile[array_rand($openFile)];
> #display the entry
> echo "<img src='$random_image' height='170' width='100'></img>";
> ?>
>
>
> This works beautifully, but my problem is that sometimes, the same image
> displays twice or more on the same page-load. My page requires 10
> different
> places where my images need to load, but I can't have any duplicate images
> show up.
>
> Any suggestions on what I need to modify here?
>
> Thanks in advance!
> Mike
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux