tags:

views:

111

answers:

3

I'm allowing users to register on my site for a 'game'; as is normal, they all receive unique IDs. At a given point in time, registration will cease. At that time, I would like to run code to assign partners, but not pairs.

So, if I had the following:

+--------+-------+
| user_id | partner |
+--------+-------+
| 1 | |
| 3 | |
| 7 | |
| 11 | |
| 12 | |
| 18 | |
+--------+-------+

what would I do to end up with something like this:

+--------+--------+
| user_id | partnr |
+--------+--------+
| 1 | 7 |
| 3 | 12 |
| 7 | 18 |
| 11 | 1 |
| 12 | 11 |
| 18 | 3 |
+--------+--------+

1,7,18,3,12,11;1,7,18,3,12,11

$query = "SELECT users FROM tabl";
$result = mysql_query ($query)
    or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');

while ($row = mysql_fetch_array($result)) {
    $users[] = $row[0];
}

$current = end($users);
$partners = array();
foreach ($users as $user)
{
        $partners[$user] = $current;
        $current = $user;
}
print_r($partners);

This seems to work, thanks to Sjoerd, but I need to write it back to the partner column.

+4  A: 
  • Retrieve the list of users.
  • Shuffle this list (optional).
  • Assign each user the next one in the list as partner, where the last user gets the first user as partner.

Code to loop through the array and assign partners:

$users = array('john', 'jack', 'jones', 'joelle', 'jesica');

$current = end($users);
$partners = array();
foreach ($users as $user)
{
        $partners[$user] = $current;
        $current = $user;
}
print_r($partners);
Sjoerd
I thought about this, but I could not find in any of my books or online how to code that. If nobody has time to write it out, I understand, but could someone point me in the direction of which functions to use? I would prefer to shuffle, however, I do not think that is wise, as I do not want to risk a user being his own partner. If there is a way to randomize with each new 'game' without the risk of a user being his own partner, that would be fantastic.
David
google "shuffle list code". There seems to be suggestions for a number of languages. If you follow the instructions in the above answer, there is no risk of a user being assigned to himself as a partner - except if there is only one user.
Boris
What would this look like? Would it be possible to help me build off of the framework below?<?php$query = "SELECT user_id FROM table" ;$results = mysql_query($query) or die(mysql_error());$array = array();while ($row = mysql_fetch_assoc($results)){ $array[] = $row;}shuffle($array);print_r($array);?>
David
I've edited my answer with a code example of how you would loop through the array and assign partners.
Sjoerd
If you shuffle the list and then assign each player the next as a partner, the next might be himself...
Hinek
@Hinek Impossible.
Sjoerd
@Sjoerd ... ok, now I understand, you shuffle the list and assign the partner from the shuffled list to the shuffled list ... I thought before, you would use the original list and assign partners in the shuffled list ... maybe you want to add this in the code sample ($users = shuffle($users)) for other dumb people like me ;-)
Hinek
Sjoerd, your solution works except that it is entirely predictable. I added an ORDER BY RAND() to achieve my desired result. Thank you very much! Now I just need to write it back to the table...?
David
Final part of my question resolved: http://stackoverflow.com/questions/3141919/
David
A: 

Sorry if there are mistakes ... I havn't been coding PHP for a long time ...

$i = rand(1, length($players) - 1)
for ($p = 0; $p < length($players); $p++)
{
    $partners[$p] = $players[$i];
    if (++$i > length($players))
    {
        $i = 0;
    }
}
Hinek
If $i == 0 then everybody gets himself as a partner.
Sjoerd
you're right, I fixed it, thx.
Hinek
+1  A: 
  • Select out the ids in a random order
  • Have two copies of the resulting array
  • Pop one element of the end of one array and push it on the top (ie offset the entire array by one)
  • Update the table as user_id,partner_id pairs

    $results = mysql_query("SELECT user_ids FROM -tablename- ORDER BY RAND()");
    
    
    $user_ids = array();
    $partner_ids = array();
    
    
    while($row = mysql_fetch_array($results))
    {
        $user_ids[] = $row['user_id'];
        $partner_ids[] = $rowp['user_id'];
    }
    
    
    $lastPartnerId = array_pop($partner_ids);
    array_unshift($partner_ids,$lastPartnerId);
    
    
    for($i=0;$i<count($user_ids);$i++)
    {
        mysql_query("UPDATE -tablename- SET partner_id = {$partner_ids[$i]} WHERE user_id = {$user_ids[$i]}");
    }
    
Brendan Bullen
I am receiving this error: mysql_fetch_array() expects parameter 1 to be resource, boolean given
David
This means mysql_query returns false, which means that the query failed.
Sjoerd
"SELECT user_ids ..." should be "SELECT user_id ..." therefore the failing query I guess ...
Hinek