I am trying to make a randow name drawing app for a secret santa gift exchange. I have run into some difficulties that I am unable to diagnos. The symptom is this: Some people for an unexplained reason are getting no errors, but they are getting no name drawn. They get a message that says "you have drawn " and then no name. I thought I had built the loops to grab the name pretty well. The criteria for drawing the name are to 1. Draw a name that is not the same as the person drawing (var for this is called $name) 2. Draw a random name that is in the list ( has not been deleted from teh databse because it was already drawn).
Am I going about this task the wrong way? Should I use an SQL statment to do the grunt work rather than PHP? If so can you offer some help wih that?
Many thanks and Merry Christmas!!
<?
function randomPick(){
$rand_id = rand(1,7);
return $rand_id;
}
function grabName($rand_id){
$sql = "SELECT name FROM names WHERE id='$rand_id'";
$db = mysql_connect("localhost", "log", "pass") or die("Could not connect to the database.");
mysql_select_db("db_name",$db);
$name_result = mysql_query($sql,$db) or die("There was a problem drawing the name.");
$name_row = mysql_fetch_row($name_result);
$person_name = $name_row[0];
$person_num = mysql_num_rows($name_result);
if($person_num == "1"){
return $person_name;
}else{
return FALSE;
}
}
function grabList($person_name){
#grab list if it is there
$db = mysql_connect("localhost", "log", "pass") or die("Could not connect to the database.");
mysql_select_db("db_name",$db);
$list_result = mysql_query("SELECT list FROM lists WHERE name='$person_name'",$db);
$list_row = mysql_fetch_row($list_result);
$list = $list_row[0];
$list_num = mysql_num_rows($list_result);
if($list_num == "1") {
$list_result = mysql_query("SELECT list FROM lists WHERE name='$person_name'",$db) or die("There was a problem getting the Chritmas list.");
while ($list_row = mysql_fetch_row($list_result))
$list = $list_row[0];
return $list;
}else{
return "<p>Your person has not posted their list yet.<p>";
}
}
function sendEmail($person_name,$list,$email){
#send email notification
if(mail("$email", "Secret Santa", "You have drawn $person_name as your person. Below is their Christmas list.
Christmas List
$list","From:me@someplace.com\r\nReply-to:me@someplace.com")){
return "An email has been sent to you<br> with your person's name and Christmas list.<p>";
}else{
return "There was some sort of problem sending an email with your person's name.";
}
}
function scratchOffList($person_name){
$db = mysql_connect("localhost", "log", "pass") or die("Could not connect to the database.");
mysql_select_db("db_name",$db);
if(mysql_query("DELETE FROM names WHERE name='$person_name'",$db)){
return TRUE;
}else{
return FALSE;
}
}
#draw name
do {
$name_id = randomPick();
}
while($name_id == $name);
do {
$person_name = grabName($name_id);
$list = grabList($person_name);
}
while($person_num != 0);
echo sendEmail($person_name,$list,$email);
if(scratchOffList($person_name) ===FALSE){
echo "There was a problem scrathing the person off the list.";
}
#write out info
echo "<table width='500' border='0' cellspacing='2' cellpadding='2'>
<tr>
<td align='left' valign='bottom' class='name'>You have drawn ".$person_name."!!!</b></td>";
echo "</tr>
<tr>
<td align='left' valign='bottom' class='list' colspan='2'><b>Christmas List</b><p>".$list."<p></td>
</tr>
</table>";
?>