How to extract and display data from CSV files using PHP

王林
Release: 2023-09-08 22:02:01
forward
899 people have browsed it

How to extract and display data from CSV files using PHP

In this article, we will learn how to display data from a CSV file using PHP’s 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 us understand this problem through the following example -

The Chinese translation of

Example 1

is:

Example 1

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

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

File name: 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); ?>
Copy after login
The Chinese translation of

Example 2

is:

Example 2

In this example, we will read a student CSV file containing the student's name, age, and gender and display their data in tabular form using 3 different methods (using the str_getcsv, fgetcsv, and SplFileObject methods).

The Chinese translation of

Students.csv

is:

Students.csv

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

File name: 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) . '
'; ?>
Copy after login

Conclusion

In summary, displaying data from a CSV file using PHP is a simple process, you can use the fgetcsv() function. With the help of HTML tags, we can easily display data in tabular form. 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 extract and display data from CSV files using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
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!