足球编码:
这个问题并不难,但首先你需要理解CSV中的代码。
这些可以在这里找到:
http://www.football-data.co.uk/notes.txt
以主客场比赛为基础。例如,如果你在第一排选择一支像尤文图斯这样的球队,你可以看到他们在主场比赛,最终结果“FTR”是“H”,意味着他们赢得了那场比赛。
编辑:
根据请求者的请求将实现更改为作为单个HTML文件操作。
假设您设置了一个启用PHP的web服务器,您可以将下面的代码压缩到一个文件中并在浏览器中运行。在我的情况下,我打电话给文件
test.php
localhost:8080/test.php
在浏览器中。你需要这两个文件
和
Results.csv
位于您的网站主目录。我有IISExpress浏览器,它在8080端口监听。
<!DOCTYPE HTML>
<html>
<body>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
<head>
<?php
// define variables and set to empty values
$teamIn = "";
$homeLosses = 0;
$awayLosses = 0;
$totalLosses = 0;
$winningTeams = []; // list of winning teams
if ( !($_SERVER["REQUEST_METHOD"] == "POST") )
{ // we reach here only if form was not submitted yet
$teamIn = "None";
$homeLosses = 0;
$awayLosses = 0;
$totalLosses = 0;
$winningTeams = [];
}
else
{ // we arrive here only if form is submitted
$teamIn = ucfirst($_POST["teamName"]); // make first char of teamname a capital
//---------------------------------------------------------------------------
// First read the CSV file and make an associative array based on first row titles
//------------------------------------------------------------------------------
$fileName = "Results.csv"; // CSV File name changed to "Results.csv"
$teams = $fields = array(); $i = 0;
$handle = fopen($fileName, "r");
if ($handle) {
while (($row = fgetcsv($handle, 4096)) !== false) {
if (empty($fields)) {
$fields = $row;
continue;
}
foreach ($row as $k=>$value) {
$teams[$i][$fields[$k]] = $value;
}
$i++;
}
if (!feof($handle)) {
die("Error: unexpected fgets() fail\n");
}
fclose($handle);
}
else{
die("Did not open file: ".$fileName.PHP_EOL);
}
//---------------------------------------------------------------------------
// now make a list of losses and make a list of teams that have beaten the team entered.
//------------------------------------------------------------------------------
$n = 0;
foreach ($teams as $team){
if ( $team['HomeTeam'] == $teamIn ){
if ($team['FTR'] == "A" ) {
$homeLosses++;
$winningTeams[$n]['date'] = $team['Date'];
$winningTeams[$n]['name'] = $team['AwayTeam'];
$winningTeams[$n]['location'] = "at Home";
$winningTeams[$n]['goalsFor'] = $team['FTAG'];
$winningTeams[$n]['goalsAgainst'] = $team['FTHG'];
$n++;
}
}
else if ($team['AwayTeam']== $teamIn){
if ($team['FTR'] == "H" ) {
$awayLosses++;
$winningTeams[$n]['date'] = $team['Date'];
$winningTeams[$n]['name'] = $team['HomeTeam'];
$winningTeams[$n]['location'] = "away";
$winningTeams[$n]['goalsFor'] = $team['FTHG'];
$winningTeams[$n]['goalsAgainst'] = $team['FTAG'];
$n++;
}
}
}
$totalLosses = $homeLosses+$awayLosses;
}
// end of PHP section
?>
<!â- This part is the form to enter your team --->
<!â- We submit the form to self- meaning this file --->
<h2>Get Match Results</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter Team Name: <input type="text" name="teamName">
<input type="submit" name="submit" value="Submit">
</form>
<!â- This part prints out losses summary --->
<?php
echo "<h2>".$teamIn."</h2>";
echo "<p><b>Summary of Losses</b></p>";
echo "<table>";
echo " <tr>";
echo " <th>Team</th>";
echo " <th>Away Losses</th> ";
echo " <th>Home Losses</th>";
echo " <th>Total Losses</th>";
echo " </tr>";
echo " <tr>";
echo " <td>".$teamIn."</td>";
echo " <td>".$awayLosses."</td> ";
echo " <td>".$homeLosses."</td> ";
echo " </tr>";
echo "</table>";
?>
<!â- This part prints out list of teams who beat the entered team --->
<?php
echo "<p><b>Details of losses</b></p>";
echo "<table>";
echo " <tr>";
echo " <th>Beaten By</th>";
echo " <th>Date</th> ";
echo " <th>Location</th>";
echo " <th>Goals For</th>";
echo " <th>Goals Against</th>";
echo " </tr>";
foreach ($winningTeams as $winningTeam){
echo " <tr>";
echo " <td>".$winningTeam['name']."</td>";
echo " <td>".$winningTeam['date']."</td>";
echo " <td>".$winningTeam['location']."</td>";
echo " <td>".$winningTeam['goalsFor']."</td>";
echo " <td>".$winningTeam['goalsAgainst']."</td>";
echo " </tr>";
}
echo "</table>";
?>
</body>
</html>