loveme Posted May 24, 2020 Share Posted May 24, 2020 (edited) 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 May 24, 2020 by loveme Quote Link to comment https://forums.phpfreaks.com/topic/310842-php-generate-team-based-on-seniority/ Share on other sites More sharing options...
requinix Posted May 24, 2020 Share Posted May 24, 2020 Get your members by seniority and assign them to teams in a round-robin way: first 6 are leaders, second 6 are assistants, and so on. Quote Link to comment https://forums.phpfreaks.com/topic/310842-php-generate-team-based-on-seniority/#findComment-1578353 Share on other sites More sharing options...
Barand Posted May 26, 2020 Share Posted May 26, 2020 (edited) $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"; } Edited May 26, 2020 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/310842-php-generate-team-based-on-seniority/#findComment-1578400 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.