代码之家  ›  专栏  ›  技术社区  ›  Werokk

如何根据用户输入读取csv

  •  0
  • Werokk  · 技术社区  · 6 年前

    我有一个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 回复  |  直到 6 年前
        1
  •  0
  •   bcperth    6 年前

    足球编码:

    这个问题并不难,但首先你需要理解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>