File: examples.md

package info (click to toggle)
php-league-csv 9.23.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 39,720 kB
  • sloc: php: 13,345; javascript: 80; makefile: 29; xml: 29
file content (96 lines) | stat: -rw-r--r-- 3,495 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---
layout: default
title: Examples
---

# Examples

## Parsing a document

A simple example to show you how to parse a CSV document.

```php
use League\Csv\Reader;

$csv = Reader::createFromPath('/path/to/your/csv/file.csv', 'r');

//get the first row, usually the CSV header
$headers = $csv->fetchOne();

//get 25 rows starting from the 11th row
$res = $csv->setOffset(10)->setLimit(25)->fetchAll();
```

## Exporting a database table as a CSV document

A simple example to show you how to create and download a CSV from a `PDOStatement` object

```php
use League\Csv\Writer;

//we fetch the info from a DB using a PDO object
$sth = $dbh->prepare(
    "SELECT firstname, lastname, email FROM users LIMIT 200"
);
//because we don't want to duplicate the data for each row
// PDO::FETCH_NUM could also have been used
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();

//we create the CSV into memory
$csv = Writer::createFromFileObject(new SplTempFileObject());

//we insert the CSV header
$csv->insertOne(['firstname', 'lastname', 'email']);

// The PDOStatement Object implements the Traversable Interface
// that's why Writer::insertAll can directly insert
// the data into the CSV
$csv->insertAll($sth);

// Because you are providing the filename you don't have to
// set the HTTP headers Writer::output can
// directly set them for you
// The file is downloadable
$csv->output('users.csv');
die;
```

## Importing a CSV into a database table

A simple example to show you how to import some CSV data into a database using a `PDOStatement` object

```php
use League\Csv\Reader;

//We are going to insert some data into the users table
$sth = $dbh->prepare(
    "INSERT INTO users (firstname, lastname, email) VALUES (:firstname, :lastname, :email)"
);

$csv = Reader::createFromPath('/path/to/your/csv/file.csv', 'r');
$csv->setOffset(1); //because we don't want to insert the header
$nbInsert = $csv->each(function ($row) use (&$sth) {
    //Do not forget to validate your data before inserting it in your database
    $sth->bindValue(':firstname', $row[0], PDO::PARAM_STR);
    $sth->bindValue(':lastname', $row[1], PDO::PARAM_STR);
    $sth->bindValue(':email', $row[2], PDO::PARAM_STR);

    return $sth->execute(); //if the function return false then the iteration will stop
});
```

## More Examples

- [Selecting specific rows in the CSV](https://github.com/thephpleague/csv/tree/7.2.0/examples/extract.php)
- [Querying a CSV](https://github.com/thephpleague/csv/tree/7.2.0/examples/filtering.php)
- [Creating a CSV](https://github.com/thephpleague/csv/tree/7.2.0/examples/writing.php)
- [Merging 2 CSV documents](https://github.com/thephpleague/csv/tree/7.2.0/examples/merge.php)
- [Switching between modes from Writer to Reader mode](https://github.com/thephpleague/csv/tree/7.2.0/examples/switchmode.php)
- [Downloading the CSV](https://github.com/thephpleague/csv/tree/7.2.0/examples/download.php)
- [Converting the CSV into a Json String](https://github.com/thephpleague/csv/tree/7.2.0/examples/json.php)
- [Converting the CSV into a XML file](https://github.com/thephpleague/csv/tree/7.2.0/examples/xml.php)
- [Converting the CSV into a HTML Table](https://github.com/thephpleague/csv/tree/7.2.0/examples/table.php)
- [Using stream Filter on the CSV](https://github.com/thephpleague/csv/tree/7.2.0/examples/stream.php)

> The CSV data use for the examples are taken from [Paris Opendata](http://opendata.paris.fr/opendata/jsp/site/Portal.jsp?document_id=60&portlet_id=121)