"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Display Data from CSV file using PHP?

How to Display Data from CSV file using PHP?

Published on 2024-11-01
Browse:704

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

Filename: index.php


   How to Display Data from CSV file using PHP?

How to Display Data from CSV file using PHP?

'; while (($data = fgetcsv($csvFile, 1000, ",")) !== FALSE) { echo ''; foreach ($data as $value) { echo '' . htmlspecialchars($value) . ''; } echo ''; } echo ''; fclose($csvFile); ?>

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

Filename: index.php


   How to Display Data from CSV file using PHP?

How to Display Data from CSV file using PHP?

"); // Split CSV data into rows echo '

Method 1: str_getcsv

'; echo '
'; echo ''; echo ''; foreach ($rows as $row) { $values = str_getcsv($row, ","); // Split row into values echo ''; foreach ($values as $value) { echo ''; } echo ''; } echo '
NameAgeGender
' . htmlspecialchars($value) . '
'; ?> Method 2: Combine fgetcsv() and HTML'; $csvFile = fopen('students.csv', 'r'); echo ''; echo ''; echo ''; while (($data = fgetcsv($csvFile, 1000, ",")) !== FALSE) { echo ''; foreach ($data as $value) { echo ''; } echo ''; } echo '
NameAgeGender
' . htmlspecialchars($value) . '
'; fclose($csvFile); ?> Method 3: using SplFileObject'; $csvFile = new SplFileObject('students.csv', 'r'); $csvFile->setFlags(SplFileObject::READ_CSV); echo ''; echo ''; echo ''; foreach ($csvFile as $row) { echo ''; foreach ($row as $value) { echo ''; } echo ''; } echo '
NameAgeGender
' . htmlspecialchars($value) . '
'; ?>

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.

Release Statement This article is reproduced at: https://www.tutorialspoint.com/how-to-display-data-from-csv-file-using-php If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3