So you want to display a randomly selected quote on your page, and you want to use PHP to do it. It’s dang simple, let me tell you.
Requirements
- You need PHP support on your server. An easy way to test this is to stick
<?php phpinfo(); ?>into a .php file, upload that file to your server, and load it in your browser. If you get a huge table with information about your PHP setup, then you know you have PHP support. If you get a blank page, then you don’t have PHP. :(
Howto
- Create a file with one quote per line. Save it as
quotes.txt. - The file in which you want to display your random quote must be a PHP file (i.e. it must end in .php). Open this file and stick in the following:
<?php
$quotes = "/the/full/path/to/your/file/quotes.txt";
$quotes = file($quotes);
srand((double)microtime() * 1000000);
$ranNum = rand(0, count($quotes)-1);
?>
It doesn’t matter where you put that. - In the area where you want your random quote displayed, insert the following:
<?php echo ($quotes[$ranNum]); ?>
If you want, you may take a look at my random quotes file (if you just want to read the quotes, may I suggest this prettier version).
Other Uses
You don’t have to use this for just random quotes, you know. Maybe you want to display a random image of yourself. You might use the following code:
<?php
$images = "/the/full/path/to/your/file/images.txt";
$images = file($images);
srand((double)microtime() * 1000000);
$ranNum = rand(0, count($images)-1);
echo "<img src=\"";
echo ($images[$ranNum]);
echo "\" alt=\"Random image\">";
?>
And in this case, your images.txt file wouldn’t contain one random quote per line, but one image name per line. Example:
/images/john.jpg
http://www.example.com/tom.png
me.gif
8 Comments
I wrote something very similar a while ago for showing random images/quotes (it’s super basic)..
$max = count($quote);
$count = rand(1,$max);
(and then obviously put the quotes in an array.)
I remember trying to do this with JavaScript last year and to me at the time it seemed much more complex. I love PHP, it is my friend.
This has nothing to do with this entry, but I love the new style!
That actually seems to be the best explanation I’ve gotten for the random quote… you’re so awesome!
For some reason I’m getting all the quotes on the same page, and not one random one. I copied your code exactly, I didn’t alter it at all. Do you maybe know what the problem is? If you do and you can help me, please e-mail me about it. :D
Thank you for this code though! It was a great help. ;)
I have copied and pasted as per the pages, But it don’t work. What format should the location of the file be. Relative to the random files or relative to the site?
Tim: the random quotes file should be a plain text file (ending in .txt) with one quote per line. You can put that quotes file anywhere on your site. In your PHP script, you’ll need to specify the full path (not URL, but path on your server) to your quotes file, so something like /home/tim/my_site/quotes.txt
This is a great script.
One problem I noticed that could caused the php to throw back an error … when you copy and paste, depending on your text-editor, it might paste the ” around the txt file reference incorrectly … as right-angle quotes instead of just straight-quotes.
Thanks!
can you explain the code to me?
i am a complete newbie in php so mind explaining this to me line by line? whats $quotes srand double macrotime etc etc..
desperately needing your help..thanks..