PDA

View Full Version : Random Quotes



Moonman
June 2nd, 2002, 05:48
I have this list of facts, and i want one of them randomly displayed whenever someone visits my site, how would i do this in PHP?

AlieXai
June 2nd, 2002, 06:09
<?php

$quotes = Array(
'quote one',
'quote two',
'another quote',
'and so on',
'last one'
);

srand((double)microtime()*1000000);

$count = count($quotes) - 1;
$randn = rand(0, $count);

echo $quotes[$randn];

?>

If your quotes are in a text file, each quote on their own line...



<?php

$file = '/path/to/quotes.txt';
if(file_exists($file))
{
$open = fopen($file, 'r');
$read = fread($open, filesize($file));
} else {
die('File Does Not Exist');
}

$quotes = explode("\n", $read);

srand((double)microtime()*1000000);

$count = count($quotes) - 1;
$randn = rand(0, $count);

echo $quotes[$randn];

?>

*note: I typed the above very quickly...

Moonman
June 2nd, 2002, 08:26
Cool thanks, if you want to see it in action check http://www.freehosts.org/moonman/facts/randomfacts.php it has useless facts.

megapuzik
June 2nd, 2002, 18:15
If you use an SQL DB, you can do this :

SELECT * FROM bla ORDER BY RAND()