The following
script is a portion of the
PHP script that I will be using to draw all the winners in the future. The section below is all the "meat" of the script.
[php:1]
if (empty($job))
{
// Print form for drawing options
echo "<table width='100%'>
<form action='$PHP_SELF' method='POST'>
<input type='hidden' name='job' value='select_winner'>
<tr>
<td class='tdrow1'><b>Starting User \#</b></td>
<td class='tdrow1'><input type='text' size='5' class='textinput' name='starting_user'></td>
</tr>
<tr>
<td class='tdrow1'><b>Ending User \#</b></td>
<td class='tdrow1'><input type='text' size='5' class='textinput' name='ending_user'></td>
</tr>
<tr>
<td class='tdrow1'></td>
<td class='tdrow1'><input type='submit' value='Select Random User' class='button'></td>
</tr>
</form>
</table>";
}
elseif ($job == "select_winner")
{
// The srand() functions seeds the random number generator with a large number
// to help in creating a truly random number. Using the mktime() function, it makes
// the number larger, and more unique.
srand(mktime() * 100000);
$user_level = array(
"9" => "1", // Newbie
"34" => "2", // Just Above Newbie
"99" => "3", // Aspiring Guru
"499" => "4", // Entry-Level Guru
"999" => "5", // Mid-Level Guru
"2499" => "6", // Guru
"999999" => "7", // Extreme Guru
);
/*
The following section adds one to the numbers submitted in the form. If you've
selected users 1 to start with, and 1000 to end with, the actual user id's for those
users would be 2 and 1001. Adding one number below compensates for this.
*/
$starting_user++;
$ending_user++;
/*
The following SQL Query selects the users from the database.
I have my own functions that call the database rather than the traditional
MySQL functions.
The username is obviously the username of the user.
The user_id is the numbered value of the user, to make sure they need to be drawn
this drawing
The user_posts is the number of posts that user has at the moment of drawing. This
will be used to decide how many times the user needs to be entered into the array.
*/
$get_users = sql_query("SELECT username, user_id, user_posts FROM my.table_name WHERE user_id >= '$starting_user' AND user_id <= '$ending_user' ORDER BY user_id ASC", $db);
while (list($user, $user_id, $user_posts) = sql_fetch_row($get_users, $db))
{
$this_user = "";
$finished_with_user = "NO";
while (list($max_post, $entries) = each($user_level))
{
if ($user_posts <= $max_post && $finished_with_user != "YES") // found the level they belong in
{
echo "Username: <b>$user</b> Posts: <b>$user_posts</b> Entries in Contest: <b>$entries</b>";
$finished_with_user = "YES";
for ($p = 1; $p <= $entries; $p++)
{
if (empty($this_user))
{
$this_user = $user;
}
else
{
$this_user = $this_user. ",". $user;
}
}
}
}
reset($user_level); // Resets the user_level array to be gone through again
if (!empty($users_for_drawing))
{
// This section adds the next users username into a string.
$users_for_drawing = $users_for_drawing. ",". $this_user;
}
else
{
// This section will only happen once on the first user entered into the string
$users_for_drawing = $this_user;
}
}
// This creates an array of all the usernames that have been pulled.
$user_array = explode(",",$users_for_drawing);
/*
The following for statement takes the shuffle() function, which randomizes the names, and runs
it 10 times to make sure it's really super-shuffled!!!
*/
for ($i = 1;$i <= 10;$i++)
{
shuffle($user_array);
}
// We will re-seed the number generator here before selecting a random user for the winner
srand(mktime() * 9847533218872169841);
// Now we create the winner, and assign it to the variable "$winner"
// The $winner recieves a value that is the numerical key for the array.
$winner = array_rand($user_array);
echo "<br /><br /><b>And the Winner is: $user_array[$winner]</b><br /> <br />";
// Do the drawing and announce the winner
}
[/php:1]
If anyone is into PHP, and has a better way to select a random user, and calculate the # of entries they get from the # of posts they have, please let me know!!!