A Simple Random Prize Winner Draw PHP Script Programming 23 APR 2012

If you are working with a small to medium data set and want to draw a random selection of prize winners from your collected data, this small PHP prize draw snippet works pretty well and is nice and should be pretty simple to adapt to your needs.

The idea here is to run a SQL statement to give you a list of all the unique customers in the identified data set, build two arrays, the first a keyed array with the customer’s details for printing purposes and a second containing their unique identifier ID key. The second array will be checked to ensure that only unique keys are contained, then shuffled using PHP’s built in shuffle function, before finally slicing off the desired amount of winners (remembering to pad this number ever so slightly so that we can generate both a winners list as well as a backup list for when winners can’t be contacted).

So very simple, but as you will find, pretty damn effective.

In this sample code, my database contains surveyors in a surveyors table and survey data in a surveys table.

$detailed = array(); //full surveyor account details, i.e. name and telephone
$fulllist = array(); //resulting list containing the unique keys retrieved by the SQL statement
$winners = array(); //final winners array
$backup = array(); //backup winners array

$winnerslength = 10; //the number of winners we desire
$backuplength = round(($winnerslength / 2), 0, PHP_ROUND_HALF_UP); //number of backup winners, in this case 5

//example SQL to give us a list of surveyors. Normally you would at this point add your filter requirements, like date range, data filters, etc in the WHERE clause.
$tempwinners = mysql_query("SELECT DISTINCT sa.`id`, sa.`name`,sa.`telephone` FROM `surveys` AS s LEFT JOIN `surveyor-accounts` AS sa ON s.`surveyor-id` = sa.`id`);

//populate both the keyed array containing all the surveyor details, as well as our workhorse array which contains only the surveyor IDs
    while ($tempwinner = mysql_fetch_assoc($tempwinners)) {
        $detailed[$tempwinner['id']] = $tempwinner;
        $fulllist[] = $tempwinner['id'];

//one last check to make sure we are unique

//shuffle the winners

//if the winners pool is smaller or equal to the desired winners count, use everything
    if (count($fulllist) <= $winnerslength) {
        foreach ($detailed as $detailedrow) {
            $winners[] = $detailedrow;
    } else {
//slice off our winners and backup winners from the shuffled list
        $temps = array_slice($fulllist, 0, ($winnerslength + $backuplength));
        $runner = 0;
//populate the arrays
        foreach ($temps as $detailedrow) {
            if ($runner < $winnerslength) {
                $winners[] = $detailed[$detailedrow];
                $runner = $runner + 1;
            } else {
                $backup[] = $detailed[$detailedrow];

//and voila, you now have a randomly drawn winners and backup winners array!
echo '<pre>';
echo '<p>Winners List</p>';
echo '<p>Backup Winners</p>';
echo '</pre>';

And that’s that. Simple, but effective.

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.