ChurchMedia
12-14-2006, 02:48 AM
Hi -- I'm trying to finish up a script that will pull user ids from a table and assign random ids from the same table to them -- like this:
1 -- 5
2 -- 3
3 -- 2
4 -- 1
5 -- 4
The trick is that a number can't be assigned to itself and it can't be assigned twice. Here is the code I have. It probably doesn't make sense to you, but hopefully you can figure out where I need to patch it up to make it randomize correctly. Right now it's not assigning numbers to themselves, but it is assigning numbers more than once. Thanks for any help you can give!
// ###################### Assign #######################
if ($_REQUEST['do'] == 'assign') {
echo "<table>";
$qry = $db->query_read("SELECT * FROM {$tbl_main} ORDER BY userid ASC");
$totalsantas = mysql_num_rows($qry);
while ($uinfo = $db->fetch_array($qry))
{
$get_from = $uinfo['get_from'];
$give_to = $uinfo['give_to'];
$uid = $uinfo['userid'];
$qry2 = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = $uid");
$uname_from = $qry2['username'];
$qry_r = $db->query_first("SELECT * FROM {$tbl_main} ORDER BY RAND()");
$uid2 = $qry_r['userid'];
$get_from2 = $qry_r['get_from'];
if (!$get_from = $get_from2 AND !$uid = $uid2){
$db->query_write ("UPDATE {$tbl_main} SET give_to='$give_to',get_from='$get_from' WHERE userid='$uid'");
$qry3 = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = $uid2");
$uname_to = $qry3['username'];
echo "
<tr>
<td>$uname_from ($uid) is assigned to $uname_to ($uid2) as Secret Santa.<td>
</tr>
";
}
}
echo "
<tr>
<td>Total Santas: $totalsantas<td>
</tr>
</table>
";
}
1 -- 5
2 -- 3
3 -- 2
4 -- 1
5 -- 4
The trick is that a number can't be assigned to itself and it can't be assigned twice. Here is the code I have. It probably doesn't make sense to you, but hopefully you can figure out where I need to patch it up to make it randomize correctly. Right now it's not assigning numbers to themselves, but it is assigning numbers more than once. Thanks for any help you can give!
// ###################### Assign #######################
if ($_REQUEST['do'] == 'assign') {
echo "<table>";
$qry = $db->query_read("SELECT * FROM {$tbl_main} ORDER BY userid ASC");
$totalsantas = mysql_num_rows($qry);
while ($uinfo = $db->fetch_array($qry))
{
$get_from = $uinfo['get_from'];
$give_to = $uinfo['give_to'];
$uid = $uinfo['userid'];
$qry2 = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = $uid");
$uname_from = $qry2['username'];
$qry_r = $db->query_first("SELECT * FROM {$tbl_main} ORDER BY RAND()");
$uid2 = $qry_r['userid'];
$get_from2 = $qry_r['get_from'];
if (!$get_from = $get_from2 AND !$uid = $uid2){
$db->query_write ("UPDATE {$tbl_main} SET give_to='$give_to',get_from='$get_from' WHERE userid='$uid'");
$qry3 = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = $uid2");
$uname_to = $qry3['username'];
echo "
<tr>
<td>$uname_from ($uid) is assigned to $uname_to ($uid2) as Secret Santa.<td>
</tr>
";
}
}
echo "
<tr>
<td>Total Santas: $totalsantas<td>
</tr>
</table>
";
}