How to Display Data from CSV file using PHP?

WBOY
Release: 2024-08-28 10:35:06
Original
885 people have browsed it

How to Display Data from CSV file using PHP?

In this article, we will learn how to display data from a CSV file using PHP using fgetcsv(), str_getcsv and SplFileObject functions.

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.

Let’s understand this with the help of an example below −

Example 1

In this example, we will read a data CSV file using fgetcsv() function and display the values in a tabular format using HTML table tag.

The CSV file used in this example −

Data.csv

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

Filename: 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>
Copy after login

Example 2

In this example, we will read a Students CSV file containing their Name, Age, and Gender, and we will display their data in the tabular format using 3 different methods, using str_getcsv, fgetcsv, and SplFileObject methods respectively.

Students.csv

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

Filename: 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, "<br>"); // 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>';
  ?>
Copy after login

Conclusion

In conclusion, displaying data from a CSV file using PHP is a straightforward process using the fgetcsv() function. With the help of HTML tags, we easily displayed the data in a tabular format. By following the examples provided in this article, we learned to read and display data from CSV files in PHP, which can be useful in various applications.

The above is the detailed content of How to Display Data from CSV file using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!