summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-02-07 09:44:37 +0100
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-02-07 09:44:37 +0100
commitbfb61f0f466bba7df3fbc09208a4f40c3460276b (patch)
treefc2576d171d7e3a21d23be140f6261eee1c5896a /examples
parent49f0de4e04c074eae1672fe6dc89717ac271cc64 (diff)
downloadcsv-bfb61f0f466bba7df3fbc09208a4f40c3460276b.zip
csv-bfb61f0f466bba7df3fbc09208a4f40c3460276b.tar.gz
csv-bfb61f0f466bba7df3fbc09208a4f40c3460276b.tar.bz2
bug fix AbstractCsv::createFromString and Writer::insertOne to better works with string conversion
Diffstat (limited to 'examples')
-rw-r--r--examples/README.md1
-rw-r--r--examples/example05.php18
-rw-r--r--examples/example06.php74
3 files changed, 75 insertions, 18 deletions
diff --git a/examples/README.md b/examples/README.md
index 85eb91a..4ed3acf 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -8,6 +8,7 @@ Examples
* [Selecting a specific row in the CSV](example03.php)
* [Filtering a CSV](example05.php) using the `Bakame\Csv\Reader` class
* [Creating a CSV](example05.php) using the `Bakame\Csv\Writer` class
+* [Passing a CSV from writing mode to Reader mode](example06.php)
The CSV use for the example is from [Paris Opendata](http://opendata.paris.fr/opendata/jsp/site/Portal.jsp?document_id=60&portlet_id=121)
diff --git a/examples/example05.php b/examples/example05.php
index e98a2e9..74cbffc 100644
--- a/examples/example05.php
+++ b/examples/example05.php
@@ -37,17 +37,6 @@ $headers = $inputCsv->fetchOne(0);
$writer = new Writer(new SplTempFileObject); //because we don't want to create the file
$writer->insertOne($headers);
$writer->insertAll($res);
-
-//we create a Reader object from the Writer object to filter the resulting CSV
-$reader = $writer->getReader();
-$names = $reader
- ->setFilter(function ($row, $index) {
- return $index > 0; //we don't want to select the header
- })
- ->setSortBy(function ($row1, $row2) {
- return strcmp($row1[0], $row2[0]); //we are sorting the name
- })
- ->fetchCol(0); //we only return the name column
?>
<!doctype html>
<html lang="fr">
@@ -64,13 +53,6 @@ $names = $reader
<?=$writer?>
</pre>
<p><em>Notice that the delimiter have changed from <code>;</code> to <code>,</code></em></p>
-<h3>Here's the firstname ordered list</h3>
-<ol>
-<?php foreach ($names as $firstname) : ?>
- <li><?=$firstname?>
- <?php
-endforeach;
-?>
</ol>
</body>
</html>
diff --git a/examples/example06.php b/examples/example06.php
new file mode 100644
index 0000000..b62d491
--- /dev/null
+++ b/examples/example06.php
@@ -0,0 +1,74 @@
+<?php
+
+error_reporting(-1);
+ini_set('display_errors', 'On');
+
+use Bakame\Csv\Writer;
+
+require '../vendor/autoload.php';
+
+$rawCsv = <<<EOF
+Anatole;31;M;2004
+Andre;13;M;2004
+Andrea;33;F;2004
+Andrea;20;F;2004
+Andy;19;M;2004
+Ange;15;M;2004
+Angela;9;F;2004
+Angèle;29;F;2004
+Angelina;8;F;2004
+Angelina;7;F;2004
+Angelique;13;F;2004
+Angelo;9;M;2004
+Ania;7;F;2004
+Anis;33;M;2004
+Anissa;21;F;2004
+Anna;117;F;2004
+Annabelle;14;F;2004
+Anne;10;F;2004
+Anouk;48;F;2004
+Anthony;41;M;2004
+Antoine;248;M;2004
+Anton;16;M;2004
+EOF;
+
+$writer = Writer::createFromString($rawCsv); //we are creating a CSV from a raw string
+$writer->setDelimiter(';');
+$writer->insertOne('Ben;7;M;2004'); //because we specified the delimiter to ";" the string delimiter MUST also be ";"
+$writer->insertAll([
+ 'Benjamin;118;M;2004',
+ ['Benoit', '6', 'M', '2004'] //because we a inserting an array the delimiter is not necessary
+]);
+
+//we create a Reader object from the Writer object to filter the resulting CSV
+$reader = $writer->getReader();
+$names = $reader
+ ->setSortBy(function ($row1, $row2) {
+ return strcmp($row1[0], $row2[0]); //we are sorting the name
+ })
+ ->fetchCol(0); //we only return the name column
+?>
+<!doctype html>
+<html lang="fr">
+<head>
+ <meta charset="utf-8">
+ <title>Example 2</title>
+</head>
+<body>
+<h1>Example 4: Using Writer object with Strings</h1>
+<h3>The table representation of the csv to be save</h3>
+<?=$writer->toHTML();?>
+<h3>The Raw CSV as it will be saved</h3>
+<pre>
+<?=$writer?>
+</pre>
+<h3>Here's the firstname ordered list</h3>
+<ol>
+<?php foreach ($names as $firstname) : ?>
+ <li><?=$firstname?>
+ <?php
+endforeach;
+?>
+</ol>
+</body>
+</html>