PHP Code For Random Image PickerEarlier this month, I got a wild hair and decided to change the name of the site to “That Reminds Me Of A Story…” In making this change, I changed the description of the site (which appears in the upper right corner of the page) to “Ask me what time it is and I’ll tell you how to build a watch.” I initially liked the change, but in the comments to that post Chris suggested that the title and description were too similar and that he preferred mashby.com as the title. I felt the same way and decided to revert back to the original title.

After the change back, I kind of missed the saying “Ask me what time it is and I’ll tell you how to build a watch.”, yet I didn’t want to lose the main description of the site “Michael Ashby’s (aka mashby) weblog regarding technology, movies, travel and other odd rants and ramblings.” That gave me the idea of adding a little chaos to the site by adding a PHP script to select random text and display it where the description is. The entry explains how I did it.


Choosing A Script
There is a PHP script that I found a few years back called “Random Image Picker” by Richard Heyes. It’s a simple little script that searches a specific file and randomly displays one line from it. Initially I used it to randomly display images for a client site. If you go to www.nysais.org and look in the upper right corner, you’ll see a random image appear. Each time you refresh, a new image is randomly selected and appears. Pretty cool huh? Well, after inspecting the script, I realized that instead of images, I could just as easily display text, so I decided to use this script. 🙂

I searched to find the script again, so that I could make it easy for you to download, but I came up with nothing. So, I’ve zipped it up, along with the random text file I used and you can download it at this link. If you don’t like this script, or if you simply want to find another one, I recommend checking out PHP Resource Index or HotScripts.com.

Step-By-Step Instructions
The following instructions assume that you’re using “Random Image Picker” and walk you through each step that I did to configure, install and run this script.

1. Download Random Image Picker.

2. Unzip the file using Winzip, Stuffit, or your preferred choice of compression software.

3. Open the file title “random.image.PHP” in a text editor. I strongly recommend using a text editor and not FrontPage, MS Word, or any other GUI HTML editor. For Windows users, this means you should use Notepad (Start Run notepad OK).

4. On line 12, you’ll see the following:

"$filename = 'http://www.YOURDOMAIN.com/random.txt';"

change the url to your domain name. So if you’re domain name is foo.com, the line should read

[CODE]$filename = ‘http://www.foo.com/random.txt’;[CODE]

5. Save the file and close notepad.

6. Upload random.image.PHP anywhere on your web site. I’m a bit anal retentive, so I put it in a /scripts/random/ folder, but you can put it in your home directory if you like. It doesn’t matter. If you’re familiar with FTP great, if you’re not, simply log into your MT control panel and select “Upload A File”. Browse to where you saved the file and then OK. With the rest of the instructions, I’m going to assume that you uploaded the script into your root directory (http://www.foo.com/random.image.php).

7. Log into MT, choose your weblog, and select “Templates”.

8. Select “Create new index template”. Title it anything you like, but the “output file” field should be “random.txt”. In the body you’ll need to put in the stuff you want random. Look in the included random.txt that I put in the zip file to see what I used. All you have to remember is that each item should be it’s own distinct line. If you’d like to have random images instead of random text, simply put in the HTML code needed to display an image on your site. For example,

<img src="/images/foo.gif" width="150" height="150" border="0" alt="foo" />

Again, just make sure that each item is on it’s own distinct line. When you’re done select the “Save” button.

9. Now for the last step. Simply find the template that you want to place the random text in. In my case, this was a template I created called “header.html” that I include on all my templates. You might be editing your Main Index page, the choice is yours. Once you’ve opened and selected your template, all you have left to do is add an include to where you want your random text to appear. This can be done a number of ways.

If you’re using PHP, then add the following line to your template:

<?PHP include('http://www.YOURDOMAIN.com/random.image.php'); ?>

If you’re not using PHP, then you can use the following MT Template tag:

<$MTInclude file="random.image.PHP"$>

You’re Done!
That’s all there is to it! Now you can edit your random.txt file via MT whenever you want to add or delete anything from what is randomly displayed. For example, you might want to post random quotes on your web site and as you come across more quotes, you can simply insert them in the Random Text template you created.

If you’ve found this tutorial helpful, please let me know by posting a comment to this entry. I’d love to see how you’re using this simple little script on your site.