Jared Stenquist » Posts in 'programming' category

Printing a randomly ordered list from an array with php

A recent project of mine required that I print out a link of online stores in a random order, so that no store has more exposure at the top of the list than others. This is the solution I came up with using PHP.

<?
// all of your items to shuffle go here
$links = array('<a href="http://www.google.com">Google</a>',
                 '<a href="http://www.yahoo.com">Yahoo</a>',
                 '<a href="http://www.ebay.com">EBay</a>',
                 '<a href="http://www.cnn.com">CNN</a>',
                 '<a href="http://www.wsj.com">Wall Street Journal</a>'                
                 );

// this shuffles up the array order
shuffle($links);

// let's create a unordered list to display the items
    echo "<ul>";
    
// here's where all the items get printed    
foreach ($links as $link) {
    echo "<li>$link</li>";
    }

// closing the unordered list    
    echo "</ul>";
?>
Posted in programming