首頁  >  文章  >  後端開發  >  使用PHP從CSV檔案擷取和顯示資料的方法

使用PHP從CSV檔案擷取和顯示資料的方法

王林
王林轉載
2023-09-08 22:01:02959瀏覽

使用PHP從CSV檔案擷取和顯示資料的方法

在本文中,我們將學習如何使用PHP的fgetcsv()、str_getcsv和SplFileObject函數從CSV檔案顯示資料。

CSV file is a simple file format used to store data with comma-separated values, and each row in it represents a record in the tabular data. To read a CSV file using PHP, we will use the fgetcsv() function which reads a line from a CSV file and returns an array of values representing the CSV data present in that line.

讓我們透過下面的例子來理解這個問題 -

Example 1

的中文翻譯為:

範例1

#在這個例子中,我們將使用fgetcsv()函數讀取一個資料CSV文件,並使用HTML表格標籤以表格形式顯示這些值。

這個例子中使用的CSV檔−

Data.csv

Name, Age, City
John, 30, New York
Mary, 25, Los Angeles
Tom, 40, Chicago

檔名:index.php

<html lang="en">
<head>
   <title>How to Display Data from CSV file using PHP?</title>
</head>
<body>
  <h3>How to Display Data from CSV file using PHP?</h3>
  <?php
    $csvFile = fopen('Data.csv', 'r');

    echo '<table>';
    while (($data = fgetcsv($csvFile, 1000, ",")) !== FALSE) {
      echo '<tr>';
      foreach ($data as $value) {
        echo '<td>' . htmlspecialchars($value) . '</td>';
      }
      echo '</tr>';
    }
    echo '</table>';

    fclose($csvFile);
	?>
</body>
</html>

Example 2

的中文翻譯為:

範例2

在這個範例中,我們將讀取一個包含學生姓名、年齡和性別的學生CSV文件,並使用3種不同的方法(使用str_getcsv、fgetcsv和SplFileObject方法)以表格形式顯示他們的資料。

Students.csv

的中文翻譯為:

Students.csv

Name,Age,Gender
John Doe,25,Male
Jane Smith,30,Female
Bob Johnson,40,Male
Sara Lee,22,Female

檔名:index.php

<html lang="en">
<head>
   <title>How to Display Data from CSV file using PHP?</title>
</head>
<body>
  <h3>How to Display Data from CSV file using PHP?</h3>
  
  <!-- Method 1: str_getcsv -->
  <?php
    $csvData = file_get_contents('students.csv');
    $rows = str_getcsv($csvData, "</p><p>"); // Split CSV data into rows
    
    echo '<h4>Method 1: str_getcsv</h4>';
    echo '<table>';
    echo '<thead><tr><th>Name</th><th>Age</th><th>Gender</th></tr></thead>';
    echo '<tbody>';
    
    foreach ($rows as $row) {
      $values = str_getcsv($row, ","); // Split row into values
      echo '<tr>';
      foreach ($values as $value) {
        echo '<td>' . htmlspecialchars($value) . '</td>';
      }
      echo '</tr>';
    }
    
    echo '</tbody></table>';
  ?>
  
  <!-- Method 2: Combine fgetcsv() and HTML -->
  <?php
    echo '<h4>Method 2: Combine fgetcsv() and HTML</h4>';
    $csvFile = fopen('students.csv', 'r');
    echo '<table>';
    echo '<thead><tr><th>Name</th><th>Age</th><th>Gender</th></tr></thead>';
    echo '<tbody>';
    while (($data = fgetcsv($csvFile, 1000, ",")) !== FALSE) {
      echo '<tr>';
      foreach ($data as $value) {
        echo '<td>' . htmlspecialchars($value) . '</td>';
      }
      echo '</tr>';
    }
    echo '</tbody></table>';
    fclose($csvFile);
  ?>
  
  <!-- Method 3: using SplFileObject -->
  <?php
    echo '<h4>Method 3: using SplFileObject</h4>';
    $csvFile = new SplFileObject('students.csv', 'r');
    $csvFile->setFlags(SplFileObject::READ_CSV);
    
    echo '<table>';
    echo '<thead><tr><th>Name</th><th>Age</th><th>Gender</th></tr></thead>';
    echo '<tbody>';
    
    foreach ($csvFile as $row) {
      echo '<tr>';
      foreach ($row as $value) {
        echo '<td>' . htmlspecialchars($value) . '</td>';
      }
      echo '</tr>';
    }
    
    echo '</tbody></table>';
  ?>

Conclusion

總之,使用PHP顯示來自CSV檔案的資料是一個簡單的過程,可以使用fgetcsv()函數。透過HTML標籤,我們可以輕鬆地以表格形式顯示資料。透過遵循本文提供的範例,我們學會了在PHP中讀取和顯示來自CSV檔案的數據,這在各種應用中都很有用。

以上是使用PHP從CSV檔案擷取和顯示資料的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除