Jump to content

Help with cycling through a list...


d1s0wn3d

Recommended Posts

Hi! This'll be my first post...

 

I'm new to PHP so please be gentle. :)

 

So here's my conundrum:

 

I need to cycle through a list, going back and forth as necessary.

 

What I want to do is, count every x users on the list and "assign" them to the first user in my list. For the second, count every x users, assign them to the second user, etc. After the first user, the value of x will not change.

 

For example:

 

If there are 25 users in the list, then starting with the first user on the list, count the 6 people under him, and "assign" them to him, for the second user, count the 8 users under him but exclude the 6 already assigned to the first user, for the third user, count the 8 users under him but exclude the 6 already assigned to the first user, and the 8 already assigned to the second user, for the fourth user, since the number of users won't equal 8, assign whoever's left and if someone new gets added in the future, pick up from there.

 

I hope I made my point clear...

 

This is my code to get the list:

    $sql = "SELECT u.* FROM `users` u WHERE u.Active = 1";
    
    $res = Query($sql);
    
    if (Rows($res) > 0) {
      while ($temp = Fetch($res)) {
        $id = $temp["UserId"];

        //Code removed because not relevant
      }
    }

Thanks!

Link to comment
Share on other sites

Is this what you are describing for 25 users?

+------------+-----+-----+-----+
| player     |  1  |  8  | 17  |
+------------+-----+-----+-----+
|            |  2  |  9  | 18  |
|            |  3  | 10  | 19  |
|            |  4  | 11  | 20  |
| assigned   |  5  | 12  | 21  |
|            |  6  | 13  | 22  |
|            |  7  | 14  | 23  |
|            |     | 15  | 24  |
|            |     | 16  | 25  |
+------------+-----+-----+-----+

If it is, then

$users = range(1,25);
$assigned = [];

    // first user
assign($users, $assigned, 6);
    // remaining users
while ($users) {
    assign($users, $assigned, ;
}



function assign(&$users, &$assigned, $n)
{
    $u = $users[0];
    $assigned[$u] = array_slice($users,1,$n);  // assign next n users to first in list
    $users = array_slice($users,$n+1);         // reduce the users array
}

Link to comment
Share on other sites

Thanks for replying Barand!

 

Actually, this is what I was describing:

+------------+-----+-----+-----+
| player     |  1  |  2  |  3  |
+------------+-----+-----+-----+
|            |  2  |  8  | 16  |
|            |  3  |  9  | 17  |
|            |  4  | 10  | 18  |
| assigned   |  5  | 11  | 19  |
|            |  6  | 12  | 20  |
|            |  7  | 13  | 21  |
|            |     | 14  | 22  |
|            |     | 15  | 23  |
+------------+-----+-----+-----+

Thanks!

Link to comment
Share on other sites

Modified accordingly

$orig = range(1,25);
$pool = $orig;          // need a copy to assign from
$assigned = [];
$u = 0;
    // first user
assign($pool, $assigned, $orig[$u], 6);
    // remaining users
while ($pool) {
    ++$u;
    assign($pool, $assigned, $orig[$u], ;
}

echo '<pre>',print_r($assigned, true),'</pre>';


function assign(&$users, &$assigned, $to, $n)
{
    $assigned[$to] = array_slice($users,1,$n);
    $users = array_slice($users,$n);
}

Gives

$assigned Array
(
    [1] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 4
            [3] => 5
            [4] => 6
            [5] => 7
        )

    [2] => Array
        (
            [0] => 8
            [1] => 9
            [2] => 10
            [3] => 11
            [4] => 12
            [5] => 13
            [6] => 14
            [7] => 15
        )

    [3] => Array
        (
            [0] => 16
            [1] => 17
            [2] => 18
            [3] => 19
            [4] => 20
            [5] => 21
            [6] => 22
            [7] => 23
        )

    [4] => Array
        (
            [0] => 24
            [1] => 25
        )

)
Link to comment
Share on other sites

  • 4 weeks later...

Hi Barand! Sorry for the delayed feedback. I had some personal stuff to attend to.

 

Anyway, your code works perfectly, although, your code doesn't seem to pick-up where it left off.

 

I'm assuming that I should put a check in the while loop to check who the last user was and how many were assigned right? Or should it be done to the assign function?

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.