How to randomly assign a partner?
- by David
I asked a question some time ago about creating a random circular partner assignment using php and mysql. This is a related issue.
I am working from the following code to try to give two users new, randomly selected partners:
$q = "SELECT user_id FROM users WHERE partner='$quit_partner' AND status='1'";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
while ($row = mysqli_fetch_array($r)) {
$users[] = $row[0];
}
$current = end($users);
$partners = array();
foreach ($users as $user)
{
$partners[$user] = $current;
$current = $user;
$q = "UPDATE users SET partner='{$partners[$user]}' WHERE user_id='{$user}'";
mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
}
Basically, a particular user (lets say user #4) quits the activity, leaving multiple other users without a partner (hypothetically, users # 5,6,7). I need to find out who those users are, hence the first query. Once I find them, I throw them into an array.
Then comes the difficult part. I want those newly partnerless users (5,6,7) to be randomly assigned new partners from everyone in the table.
The current code is flawed in that it only assigns the newly partnerless users eachother.
Thanks for your help.