M Moonman Well-Known Member NLC Jun 2, 2002 #1 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?
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?
A AlieXai New Member Jun 2, 2002 #2 Code: <?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... Code: <?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... Last edited: Jun 2, 2002
Code: <?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... Code: <?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...
M Moonman Well-Known Member NLC Jun 2, 2002 #3 Cool thanks, if you want to see it in action check http://www.freehosts.org/moonman/facts/randomfacts.php it has useless facts.
Cool thanks, if you want to see it in action check http://www.freehosts.org/moonman/facts/randomfacts.php it has useless facts.
megapuzik New Member Jun 2, 2002 #4 If you use an SQL DB, you can do this : PHP: SELECT * FROM bla ORDER BY RAND()