خرید بک لینک

Vote count: 0

I have a table of available teams teams, with 24 different options.

I have another table entries, where each row is an allocation of one team to a user.

When an entry is created, a random team that has not been picked is allocated. However, if all the teams have been allocated (this can happen multiple times), only teams not yet allocated in this round of allocation are available.

For example, if my teams are A, B, C and D:

  • If there is an entry for A in entries, only B, C and D are available
  • If A, B, C and D have been picked, they are all available again
  • IF A has 3 entries, B has 3 entries, C has 2 entries and D has 2 entries, only C and D are available, until they all have the same number of entries

My code for this is convoluted:

//Make array of teams
for($i=1;$i<=24;$i++) $team[$i] = 1;

//Get entries from database
$stmt = $dbh->prepare("SELECT `team` FROM `entries`");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

//Create array of available teams
$numRows = $stmt->rowCount();
while($numRows >= 24) {
    for($i=1;$i<=24;$i++) {
        $team[$i] = $team[$i]+1;
    }
    $numRows = $numRows - 24;
}

//Remove entries for teams in array
foreach($rows as $row) $team[$row["team"]] = $team[$row["team"]]-1;
foreach($team as $i => $v) if($v > 0) $available[] = $i;

There must be a more straightforward method to accomplish this; how can this be done?

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 19 خرداد 1395 ساعت: 16:35

صفحه بندی