List Random Authors (via PHP)

It’s been far too long since I posted to the Tweak. Sometimes it’s hard to document when you’re working, but one of my resolutions is to do better with this (especially with the phenomenal strides Movable Type has taken recently). In the mean time, you’ll probably find more frequent, shorter posts that may not have as much explanation as you might have found in the past. The hope is that getting a lot of quick solutions out there for public consumption is better than a few comprehensive ones. Enjoy.

A couple of things to note first: Makes sure that you’ve got author archives set up. You can hack this to work with author profiles, but that’s not the scope of this tutorial.

<?php
    $displayed_authors = array(); // Will hold indexes from $authors for authors already displayed
    $show = 25; // How many authors should we show?

    <mt:Authors>
        $authors[<mt:AuthorId />] = '<li><a href="<mt:EntryLink archive_type="Author" />"><mt:EntryAuthorDisplayName encode_php='1' /></a></li>';
    </mt:Authors>

    for ($i=1; $i <= $show; $i++) {
        $rn = array_rand($authors);
        // Loops until it finds an author not displayed
        while(in_array($rn, $displayed_authors)) {
            $rn = array_rand($authors);
        }
        array_push($displayed_authors, $rn);
        echo $authors[$rn];
    }
?>

First, we set up an array where we’ll store all the authors that have already been displayed. More on that in a minute. Then we define how many authors we want to show with this block.

Next, we loop through all the authors, stashing them all into an array. Actually, we’re stashing the html that we want displayed as it gets looped through. This can be basically whatever you want. If you use other Movable Type tags, make sure you add the encode_php='1' filter or else you could get some nasty PHP parse errors and break your site. (Usually because of single quote/double quote mismatching.)

Next up is displaying the authors. We’re going to loop through authors 25 times; remember, that’s what we set up with the $show variable. The array_rand function basically says “pull a random number from 1 to the number of items in our array”. We had to put in a special while loop to make sure it didn’t pick a number already used; this would result in duplicate listings of a single author. The array_push then stores the new, non-duplicate random number in our displayed authors array to ensure we don’t duplicate it next time through the loop.

Finally, we echo a random item from the $authors array. This loops through however many times you’ve told it to with the $show variable.

Thanks to Arvind for helping me solve the dupe problem. He also made me say that I will now worship him forever.