Jump to content

Recommended Posts

I would like to generate team by their seniority based on the available members.
For example : I have 60 members. I want use all this 60 members as teams. Each team has one leader,one asst leader, and 8 members (1+1+8) .

 

$result = mysqli_query($con, "SELECT * FROM `members`  ORDER BY DOJ,DOB"); 
$perteam =10; 
$teamstart=1; 
$teams=15; 
$required=$perteam*$teams; 
$data = array(); 
while( $row = mysqli_fetch_array($result)) { 
$data[]=$row; 
} 

foreach($data as $fetch){ 
echo $fetch["empname"].'<br>';
}

I used the above code to fetch the members in seniority . Now I want to add another coumns Team and Designation as follows;

1.Peter - Team 1-Leader1
2.Robin - Team 2 - Leader2
… Likewise it should go…

How do I achieve this.

any lead…?

Edited by loveme
Link to comment
https://forums.phpfreaks.com/topic/310842-php-generate-team-based-on-seniority/
Share on other sites

$res = $db->query("SELECT  name
                         , timestampdiff(MONTH, doj, curdate()) as sen
                         , timestampdiff(YEAR, dob, curdate()) as age
                    FROM MEMBER
                    ORDER BY sen DESC, age DESC
                  ");
$members = $res->fetchAll();

$titles = ['Leader', 'Assistant', 'Member 1', 'Member 2', 'Member 3', 'Member 4', 'Member 5', 'Member 6', 'Member 7', 'Member 8'];

I originally set out on the round-robin route to allocate the members to teams ...

$teams = [];
$t = 0;
foreach ($members as $r) {
    $teams[$t%6][] = $r;
    $t++;
}

... but the problem with this method is it's bias. Team 1 gets the most experienced Leader and Team 6 gets the least experienced. On the next cycle, Team 1 gets the most experienced Assistant and Team 6 again gets the least, and so on for all levels.

Therefore I'd recommend an approach which gets the players for each level then shakes the bags before allocating to teams

$ranks = array_chunk($members, 6);                             // get a chunk of 6 members for each rank
$tdata = '';
foreach ($ranks as $r => $rmembers)  {
    shuffle($rmembers);                                        // shake the bag - random allocation to teams
    $tdata .= "<tr><td class='rank'>{$titles[$r]}</td>";
    foreach ($rmembers as $m) {
        $tdata .= "<td>{$m['name']} <span class='years'>({$m['sen']}/{$m['age']})</span></td>";
    }
    $tdata .= "</tr>\n";

}

image.png.1a52795a8bed5ee0f43ce11d5a9b4002.png

Edited by Barand
  • Like 1
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.