Article
Better Anything Rotator
A couple of weeks ago, Dan Benjamin introduced his Better Image Rotator (BIR) as an new and improved way to display a random image on a web site. The script, building upon his original rotator, allows you to associate a URL, alternate text and a title with each picture. The resulting <img> tag included these extra bits. While adding a picture to the mix was much easier with the old script (just dump a file on the server et voila!), the new one is not hard. And it’s much more interesting. Photos still live in a server directory, but the associated info is in an INI file — just like the win.ini and system.ini files hanging around old versions of Windows. The file format looks like this:
[ALA]
src = img/ala.jpg
alt = ALA Logo
url = http://alistapart.com/
title = A List Apart
[Scientist]
src = img/scientist.png
alt = Pic of a scientist
url = http://hivelogic.com/
title = Hivelogic
Pretty simple, eh?
Dan’s BIR could not have arrived at a better time for me — I had been contemplating how to rotate photos with captions on the new UW Arboretum site when his article showed up on A List Apart. But still, it wasn’t quite right. See, I need to store (and rotate) not only photos and alt text, but a full caption as well.
After a few emails with Dan, I was in business. I added another line to each block in the INI configuration file, then modified the PHP script to accommodate a new field called “caption.”
But yesterday, I was looking for an easy way to rotate quotes (testimonials) on a redesign for Partners in Place. A “real” database would have worked, but using BIR was much easier. So I ripped apart the INI file, replacing four image-oriented fields with two quote-oriented ones. Then I modified the PHP file, replacing all traces of “image” with a generic “item” or “row.” The result: My Better Anything Rotator.
I’m offering the modified script for anyone who would like to use it. Things are mostly as Dan intended them, but I must make a few notes:
- It no longer outputs anything by default. Originally, the script outputted an
<img>tag, including an<a href="...">if a URL had been supplied with the selected image. Now you must edit theprintfyourself. (Don’t worry, it’s really easy — even a non-programmer like me can do it.) - Dan’s function to display an image was called
showImage(). Mine isshowItem(). - In my version, you can specify a row/item/block from the INI file. Just hit
filename.php?row=RowName, whereRowNameis the part inside the brackets (these: []) in your INI file. Does that make sense? I hope so. Leave off the question mark, etc to randomize. - Just like with the original, you can specify an INI file when you call the function. However, if you specify an INI file you must also supply a row name (or don’t supply one by using
null) like this:showItem(specify.ini, rowName);.
I probably forgot something. Anyway, take a look at my demo and feel free to use/abuse/tweak if it will help with a project. The following files are available:
- Index displays random quote
- Source: rotator.php
- Source: db.ini (the configuration file)
* * *
DISCLAIMER: I am not a programmer and have only a basic understanding of PHP. This script was created by tweaking and hacking Dan Benjamin’s work, not because I actually understand this stuff. None the less, feel free to contact me with corrections/questions/requests.