Re: multiple random items

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

 



2007. 07. 16, hétfő keltezéssel 15.40-kor chris@xxxxxxxxxxxx ezt írta:
> Hi I have this script which pulls 1 random item from a txt file.
> How would I modify it to pull 10 random items.
> 
> <?php
> $delim = "\n"; 
> $quotefile = "names.txt"; 
> $fp = fopen($quotefile, "r"); 
> $contents = fread($fp, filesize($quotefile)); 
> $quote_arr = explode($delim,$contents); 
> fclose($fp); 

$quotenum = 0;
$quotes_displayed = array();
while ($quotenum < 10) {
  while (!in_array($quote_index, $quotes_displayed)) {

> // generate random quote index 
> $quote_index = (mt_rand(1, sizeof($quote_arr)) - 1); 

  }

> // get quote at $quote_index and return it 
> $herequote = $quote_arr[$quote_index]; 
> echo "<li><a href='http://www.$herequote'>$herequote</a></li>";

  $quotes_displayed[] = $quote_index;
  $quotenum++;
}

> ?>
> 

hope that helps

greets
Zoltán Németh

> Thanks
> 

-- 
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