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.