卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章18462本站已运行340

使用PHP从CSV文件中提取和显示数据的方法

使用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>"); // 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>';
  ?>
</p>

Conclusion

总之,使用PHP显示来自CSV文件的数据是一个简单的过程,可以使用fgetcsv()函数。借助HTML标签,我们可以轻松地以表格形式显示数据。通过遵循本文提供的示例,我们学会了在PHP中读取和显示来自CSV文件的数据,这在各种应用中都很有用。

卓越飞翔博客
上一篇: 将给定字符串中的每个辅音序列替换为其长度
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏