Skip to Content

Aarron Walter

XHTML | CSS |

Categories

My Book: Building Findable Websites

Building Findable Websites: Web Standards, SEO, and Beyond
Building Findable Websites: Web Standards, SEO, and Beyond
Aarron Walter
Buy on Amazon
Book's Companion Website
Companies waste fortunes seeking a magic bullet for Search Engine Optimization. But the keys to honest, effective web findability are appropriate writing and semantic markup. Aarron Walter’s wonderfully lucid and informative book tells everything you need to know to get your web content (or your client’s) in front of as many appreciative readers as possible.

- Jeffrey Zeldman, founder, Happy Cog Studios author, Designing With Web Standards, 2nd Edition

Now playing on my computer

Bill Moyers Journal – Rage on the Radio Track | Artist
Bill Moyers Journal – Andrew J. Bacevich Track | Artist
CNN – CNN=Politics Daily Track | Artist
CNN – CNN=Politics Daily Track | Artist
The New York Times – NYT: Front Page for 10/2/2008 Track | Artist
My Playlist Feed | My Last.fm Profile

Recent Photos From Flickr

Lincoln Balogna and Washinton Cheese, Sat, 2 Aug 2008 12:58:15 -0800 George Washington Cheese, Sat, 2 Aug 2008 12:55:09 -0800
Abe Lincoln Balogna, Sat, 2 Aug 2008 12:51:37 -0800 So many choices, Thu, 17 Jul 2008 15:11:20 -0800
My Flickr Photo Stream | All Photos

High Tech Gift Exchange With PHP

27 Sep . 2006

My family has a problem. Many of us live in different places across the US, and subsequently have a hard time honoring a long Christmas tradition of ours. Each year we draw names from a hat to determine who will be the recipient of our Christmas present. We started the tradition when I was just a kid because we didn’t have a lot of money to buy everyone a gift, but it has turned into our favorite part of Christmas. Everyone closely guards the secret of the name they have drawn, and there is often espionage involved to determine who might be buying you a gift.

Since we all live in different cities now, a physical hat drawing won’t suffice anymore. To solve the problem I wrote a little PHP script that does the random drawing for us, and emails everyone the name they have drawn, preserving the secret.

Take a look at the code here.

The script creates two parallel arrays; one listing all gift exchange participant names, and the other listing their email address. Next, it runs a loop through the names array and generates a random number between 0 and the number of names, making the name selection. The trick is you should not be able to draw your own name or a name that has already been drawn. To prevent this, there is a while loop inside the name drawing loop that will redraw a name if the above criteria are not met (this was the trickiest part). Next you have to store the name drawn in an array that tracks who has already been selected so you don’t end up with someone getting more than one gift. Lastly, just send an email using the PHP Mailer class to the person telling them what name they have drawn.

It works like a charm, and lets our family tradition continue for years to come!

5 Responses to “High Tech Gift Exchange With PHP”

  1. Christopher Says:

    Almost what I’m looking for. However, there are certain people that I don’t want paired up. For example, I am already going to give my wife a present so I don’t want to be matched with her.

  2. Aarron Says:

    Christopher,
    You can add a conditional statements in the while loop to prevent drawing a name in a condition you do not want. It could be hard coded in or use another series of parallel arrays to match up people that can not draw eachother’s name.

  3. Corinne Says:

    This is great, I’m attempting to use it. The only problem is, each time I almost always get one email that is blank for the name selection. I have 9 people; might this have something to do with it? I did change it to (0,8) in the $pull line, but it’s still happening…

    TIA.

  4. Aarron Says:

    Corinne,

    Probably what is causing this problem is the for loop. If you have 9 people, the for loop will look like this:

    for($i=0;$i<9;$i++){

    $pull = rand(0,8);
    while(in_array($pull,$drawn) or $i==$pull){ $pull = rand(0,8); }
    array_push($drawn,$pull);

    etc …

    The for loop needs to have $i set to the exact number of participants int eh gift exchange, and the random draw is one less than the total, because the array indexing starts at 0, so 9 people would each be assigned a number from 0 to 8 (9 numbers). Hope that makes sense.

    A better way to do this would be to avoid hard-coding the number of participants, and having the script just count them for you. Here is what that might look like:

    for($i=0;$i

    $pull = rand(0,(count($names)-1));
    while(in_array($pull,$drawn) or $i==$pull){ $pull = rand(0,(count($names)-1)); }
    array_push($drawn,$pull);

    etc …

    Good luck, and Merry early Christmas!

  5. Corinne Says:

    Wow, thanks for the quick reply! That worked, Merry Christmas to you as well!

Share Your Thoughts

* indicates required