Simple PHP randomizer

On a regular basis I get questions about the randomized books and music albums that are displayed on this site. A common question is if I use a plugin for that, and the answer is no, I use a custom PHP script.

The script is quite simple, but since I do get questions about it I think a quick explanation of how it works is in place, if only to serve as something to refer future questions to.

The script consists of three parts. The first part is an array of arrays, with each sub array containing the href and title attributes of the link plus the src and alt attributes of the image for one book (only two array entries are displayed here):

echo '<ul>' . "\n";
$items = array(
	1 => array(
		'href' => '',
		'title' => 'Buy Blog Design Solutions from',
		'src' => '/i/r/1590595815.01.jpg',
		'alt' => 'Buy Blog Design Solutions from'
	2 => array(
		'href' => '',
		'title' => 'Buy Build Your Own Web Site the Right Way from',
		'src' => '/i/r/build-your-own-website-the-right-way.jpg',
		'alt' => 'Buy Build Your Own Web Site the Right Way from'
	/* Add as many entries as needed. */

In this case the title and alt attributes contain the same text. The image is linked, so its alt attribute should describe the link target for screen reader users and users with images disabled. The title attribute provides the same information to sighted mouse users with images enabled. I chose to do it this way in case I should want to use different text for those two attributes.

The second part randomly picks three items from the array:

$numberOfItems = 3;
$randItems = array_rand($items, $numberOfItems);

When the array_rand function is called with a second argument (3 in this case), it returns an array consisting of that number of entries, randomly picked from the array passed in the first argument.

Finally, the third part is a loop that outputs a list item containing a link and an image for each entry in $randItems:

for ($i = 0; $i < $numberOfItems; $i++) {
	$item = $arrItems[$randItems[$i]];
	echo "\t" . '<li class="r' . ($i + 1) . '"><a href="' . $item['href'] . '" title="' . $item['title'] . '"><img src="' . $item['src'] . '" alt="' . $item['alt'] . '"></a></li>' . "\n";
echo '</ul>' . "\n";

To keep the generated HTML tidy, each line begins with a tab character (\t) and ends with a newline character (\n). They aren't necessary for the HTML to work, so you may or may not want to keep it that way. The class attributes for the list items are there to let me float the first two in opposite directions with the following CSS snippet:

.r1 img {float:left}
.r2 img {float:right}

And that's it. php-randomizer.phps is a generic example file that contains all parts of the script. The script is free to use, no strings attached.

At first I was a little worried that using this script for lots of things would use a lot of processing power. That does not seem to be the case, however. I use this script (or variations of it) for four different sets of content on every page load on this site ("Recommended books", "Recommended music", "Best of 456 Berea Street", and "Random places to go") and have not noticed any performance problems despite getting plenty of traffic and being on a shared server.

As I am not a PHP expert, feel free to suggest improvements.

Posted on January 24, 2007 in PHP