How to randomly assign a partner?

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-12-30T22:12:54Z Indexed on 2010/12/30 22:54 UTC
Read the original article Hit count: 159

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql