如何根据用户输入从csv中读取

时间:2018-09-04 00:33:40

标签: php csv

我有一个包含欧洲足球队成绩的csv文件。我希望用户进入一个团队,然后希望系统显示该团队的结果,包括损失。

我设法用php读取csv文件。

如何从程序用户中读取团队名称?

如何查询CSV文件以便检索该团队的结果?

http://www.football-data.co.uk/mmz4281/1718/I1.csv中读取数据(2017/2018赛季意大利足球比赛结果,结果;比赛统计数据;比赛总进球)

到目前为止,这是我的代码,共有2个文件:

<?php

include('init.inc.php');
header('Content-type: text/plain');
print_r(read_csv('I1.csv'));
$data = read_csv('I1.csv');

<?php

function read_csv($filename){
    $rows = array();

    foreach (file($filename, FILE_IGNORE_NEW_LINES)as $line){
      $rows[] = str_getcsv($line);
    }

    return $rows;
}

function write_csv($filename, $rows){
    $file = fopen($filename, 'w');

    foreach ($rows as $row){
        fputcsv($file, $row);
    }
    fclose($file);

}

1 个答案:

答案 0 :(得分:0)

足球编码:

问题并不难,但是首先您需要了解CSV中的代码。

这些可以在这里找到: http://www.football-data.co.uk/notes.txt,并且基于主场比赛和客场比赛。例如,如果您在第一行选择像尤文图斯这样的球队,您会看到他们在主场比赛,最终结果“ FTR”为“ H”,这意味着他们赢得了比赛。

您需要先掌握CSV编码,然后才能编写任何PHP!

编辑:应请求者的要求更改了实现,以作为单个HTML文件进行操作。

假设您已经设置了启用PHP的Web服务器,则可以将下面的代码粘贴到文件中并在浏览器中运行。就我而言,我调用了文件test.php并通过在浏览器中键入URL localhost:8080/test.php来运行它。您需要在网站主目录中拥有两个文件test.phpResults.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>