-1

Group G of The Champions League the results of football competition:

RB Leipzig;AS Monaco;draw
FC Porto;Besiktas JK;loss
Besiktas JK;RB Leipzig;win
AS Monaco;FC Porto;loss
AS Monaco;Besiktas JK;loss
RB Leipzig;FC Porto;win
Besiktas JK;AS Monaco;draw
FC Porto;RB Leipzig;win
Besiktas JK;FC Porto;draw
AS Monaco;RB Leipzig;loss
FC Porto;AS Monaco;win
RB Leipzig;Besiktas JK;loss

  1. The result of the match refers to the first team listed.

    (Examples:
    Besiktas JK;RB Leipzig;win

    • means that the Besiktas JK beat the RB Leipzig.

    AS Monaco FC;Besiktas JK;loss

    • means that the Besiktas beat the AS Monaco FC.

    RB Leipzig;AS Monaco FC;draw

    • means that the RB Leipzig and AS Monaco FC tied.)
  2. A win earns a team 3 points. A draw earns 1. A loss earns 0.

  3. The outcome should be ordered by points, descending. In case of a tie, teams are ordered alphabetically.

The output should come out like this:

Team                           | MP |  W |  D |  L |  P
Besiktas JK                    |  6 |  4 |  2 |  0 |  14
FC Porto                       |  6 |  3 |  1 |  2 |  10
RB Leipzig                     |  6 |  2 |  1 |  3 |  6
AS Monaco                      |  6 |  0 |  2 |  4 |  2

However, I can't get the results in terms of rankings and away wins. How can I do it?

#tournament.txt

RB Leipzig;AS Monaco;draw
FC Porto;Besiktas JK;loss
Besiktas JK;RB Leipzig;win
AS Monaco;FC Porto;loss
AS Monaco;Besiktas JK;loss
RB Leipzig;FC Porto;win
Besiktas JK;AS Monaco;draw
FC Porto;RB Leipzig;win
Besiktas JK;FC Porto;draw
AS Monaco;RB Leipzig;loss
FC Porto;AS Monaco;win
RB Leipzig;Besiktas JK;loss
#tournament.php

<?php

$lines = file('tournament.txt');

foreach ($lines as $line) {
    
    $parts = explode(';', $line);
    
    $teams[$parts[0]][] = $parts[2];
    $teams[$parts[1]][] = $parts[2];
}


uksort($teams, function ($a, $b) use ($teams) {
    $aPoints = 0;
    $bPoints = 0;
    foreach ($teams[$a] as $result) {
        if ($result == 'win') {
            $aPoints += 3;
        } elseif ($result == 'draw') {
            $aPoints += 1;
        }
    }
    foreach ($teams[$b] as $result) {
        if ($result == 'win') {
            $bPoints += 3;
        } elseif ($result == 'draw') {
            $bPoints += 1;
        }
    }
    foreach ($teams[$a] as $result) {
        if ($result == 'loss') {
            $aPoints += 0;
            $bPoints += 3;
        }
    }
    foreach ($teams[$b] as $result) {
        if ($result == 'loss') {
            $aPoints += 3;
            $bPoints += 0;
        }
    }
    if ($aPoints == $bPoints) {
        return $a <=> $b;
    }
    return $bPoints <=> $aPoints;
});

$fp = fopen('tournament.txt', 'w');
fwrite($fp, "Team                          | MP |  W |  D |  L |  P

");
foreach ($teams as $team => $results) {
    $mp = count($results);
    $w = 0;
    $d = 0;
    $l = 0;
    foreach ($results as $result) {
        if ($result == 'win') {
            $w++;
        } elseif ($result == 'draw') {
            $d++;
        } else {
            $l++;
        }
    }
    $p = $w * 3 + $d;
    fwrite($fp, sprintf("%-30s| %2d | %2d | %2d | %2d | %2d

", $team, $mp, $w, $d, $l, $p));
}

fclose($fp);

?>
kalawaja
  • 13
  • 3

1 Answers1

1

I cannot test your code because I am in PHP Version 5.6.36. But with the code below which works from PHP 5 I get results. Including away wins.

<style type="text/css">
table       { border-spacing: 0px; }
table th    { padding: 5px; border: 1px solid black; }
table td    { padding: 3px; border: 1px solid dimgrey; }
</style>


<?
// Initialisation
$infoList = array("MP","W","Wext","D","L","P");

// Open File
$lines = file('tournament.txt');

// For Each Line
foreach ($lines as $line)
{
  $parts = explode(';', $line);
  
  // Extraction
  $teamA    = $parts[0];
  $teamB    = $parts[1];
  $score    = $parts[2];

  // Initialization
  $teamList["$teamA"]["W"]      = 0;
  $teamList["$teamA"]["Wext"] = 0;
  $teamList["$teamA"]["L"]      = 0;
  $teamList["$teamA"]["D"]      = 0;
  $teamList["$teamA"]["P"]      = 0;
  $teamList["$teamA"]["MP"]     = (array_key_exists("MP", $teamList["$teamA"]))?bcadd($teamList["$teamA"]["MP"],1,0):1;

  // Initialization
  $teamList["$teamB"]["W"]      = 0;
  $teamList["$teamB"]["Wext"] = 0;
  $teamList["$teamB"]["L"]      = 0;
  $teamList["$teamB"]["D"]      = 0;
  $teamList["$teamB"]["P"]      = 0;
  $teamList["$teamB"]["MP"]     = (array_key_exists("MP", $teamList["$teamB"]))?bcadd($teamList["$teamB"]["MP"],1,0):1;

  // Memorisation
  $matchList[] = array("teamA"=>$teamA, "teamB"=>$teamB, "score"=>trim($score));
}
// End - For Each Line



// For Each Match
foreach($matchList as $matchKey => $matchValue)
{
    // If Team A Win
    if($matchValue["score"]=="win")
    {
        // Memorisation Team A
        $teamList["".$matchValue["teamA"].""]["W"]  = bcadd($teamList["".$matchValue["teamA"].""]["W"],1,0);
        $teamList["".$matchValue["teamA"].""]["P"]  = bcadd($teamList["".$matchValue["teamA"].""]["P"],3,0);
    }


    // If Team A Loss
    if($matchValue["score"]=="loss")
    {
        // Memorisation Team B
        $teamList["".$matchValue["teamB"].""]["W"]      = bcadd($teamList["".$matchValue["teamB"].""]["W"],1,0);
        $teamList["".$matchValue["teamB"].""]["Wext"] = bcadd($teamList["".$matchValue["teamB"].""]["Wext"],1,0);
        $teamList["".$matchValue["teamB"].""]["P"]      = bcadd($teamList["".$matchValue["teamB"].""]["P"],3,0);
    }


    // If Equality
    if($matchValue["score"]=="draw")
    {
        // Memorisation Team A
        $teamList["".$matchValue["teamA"].""]["D"]  = bcadd($teamList["".$matchValue["teamA"].""]["D"],1,0);
        $teamList["".$matchValue["teamA"].""]["P"]  = bcadd($teamList["".$matchValue["teamA"].""]["P"],1,0);

        // Memorisation Team B
        $teamList["".$matchValue["teamB"].""]["D"]  = bcadd($teamList["".$matchValue["teamB"].""]["D"],1,0);
        $teamList["".$matchValue["teamB"].""]["P"]  = bcadd($teamList["".$matchValue["teamB"].""]["P"],1,0);
    }
}
// Fin - For Each Match










// -------- Display in HTML -------- //

echo "<table>";
echo "<tr>";
echo "<th></th>";
foreach($infoList as $infoKey => $infoValue)    { echo "<th>".$infoValue."</th>"; }
echo "</tr>";


// For Each Team
foreach($teamList as $teamName => $teamValue)
{
    echo "<tr>";
    echo "<td>".$teamName."</td>";

    // For Each Type Information
    foreach($infoList as $infoKey => $infoValue)
    {
        echo "<td>".$teamValue["$infoValue"]."</td>";
    }
    // End - For Each Type Information

    echo "</tr>";
}
// End - For Each Team

echo "</table>";

// --------------------------------- //
?>

It may seem a bit heavy but it allows to have a teamList array with all the necessary information

Juan
  • 690
  • 4
  • 15
  • 1
    You really should stop helping others here before helping yourself by using PHP 7.4 or later. PHP 5 is **END OF LIFE** nearly *4 years ago!!!* Update!!! – Martin Oct 10 '22 at 13:40
  • I am not the administrator of the network on which I develop. At home I'm on PHP 7.X. But I'm not at home and I took advantage of my break at work to try to help someone. With the network on which I am and which I do not administer. – Juan Oct 10 '22 at 13:49