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!
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.
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.
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.
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!
Wow, thanks for the quick reply! That worked, Merry Christmas to you as well!
$mail->$Priority = 1;
should be
$mail->Priority = 1;