Пример #1
0
 /** @todo Luke had this one commented out, figure out why & what to do */
 public function test_2_2_Other_Methods_Of_Looping_Through_A_File()
 {
     ob_start();
     $reader = new Csv_Reader($this->tmpfile);
     $i = 0;
     while (($row = $reader->getRow()) && $i < 5) {
         print $row[1] . "<br>";
         $i++;
     }
     $captured = ob_get_clean();
     $this->assertEquals('Widget<br>Whatsamahoozit<br>Dandy Doodad<br>Thingamajigger<br>Jolly Junk<br>', $captured);
 }
 function main()
 {
     $outputHandle = fopen('sampleProducts.csv', 'w');
     $this->seedCsv = fopen('products.csv', 'r');
     $fields = $this->fields();
     fputcsv($outputHandle, $fields);
     $seedRow = $this->row();
     $newCsv = '';
     $newCsv .= implode(',', $fields) . "\n";
     $products = new Csv_Reader('ekow-new.csv');
     while ($row = $products->getRow()) {
         $newRow = $seedRow;
         // images
         $newRow[11] = '/' . $row[5] . '.jpg';
         $newRow[12] = '/' . $row[5] . '.jpg';
         $newRow[13] = '/' . $row[5] . '.jpg';
         $newRow[self::SKU] = $row[9];
         $newRow[self::name] = $row[7];
         $newRow[self::description] = $row[8];
         $newRow[self::short_description] = $row[8];
         $newRow[self::price] = $row[10];
         if ($row[4]) {
             $category = $row[4];
         } else {
             if ($row[3]) {
                 $category = $row[3];
             } else {
                 if ($row[2]) {
                     $category = $row[2];
                 } else {
                     if ($row[1]) {
                         $category = $row[1];
                     }
                 }
             }
         }
         $newRow[self::category] = $category;
         fputcsv($outputHandle, $newRow);
     }
     echo 'data created to sampleProducts.csv';
 }
Пример #3
0
 public function test_Set_Header()
 {
     // the comma-200 file doesn't have a header, so it will be indexed numerically
     $reader = new Csv_Reader($this->files['comma-200']);
     $header = array('name', 'date', 'email', 'address_1', 'city', 'state', 'zip', 'country', 'phone', 'fax', 'keywords', 'order_id');
     $reader->setHeader($header);
     $row = $reader->getRow();
     $this->assertEqual(array_keys($row), $header);
     $row = $reader->current();
     $this->assertEqual(array_keys($row), $header);
     $row = $reader->next();
     $this->assertEqual(array_keys($row), $header);
     $allrows = $reader->toArray();
     $this->assertEqual(array_keys(current($allrows)), $header);
 }
Пример #4
0
 function testShouldReadUTF8Text()
 {
     $file = sys_get_temp_dir() . '/utf8.csv';
     file_put_contents($file, 'foo,ﺡ,bar');
     $reader = new Csv_Reader($file, new Csv_Dialect());
     $row = $reader->getRow();
     $this->assertEquals(array('foo', 'ﺡ', 'bar'), $row);
 }
Пример #5
0
set_include_path(realpath('../../') . PATH_SEPARATOR . get_include_path());
?>
<html>
<head>
<title>Csv Uploader</title>
</head>
<body>
<?php 
if (!empty($_FILES['csv'])) {
    require_once 'Csv/Exception/FileNotFound.php';
    require_once 'Csv/Reader.php';
    $filename = $_FILES['csv']['tmp_name'];
    try {
        echo "<table border='1'>";
        $reader = new Csv_Reader($filename);
        $row = $reader->getRow();
        echo "<tr>";
        foreach ($row as $header) {
            printf("<th>%s</th>", $header);
        }
        echo "</tr>";
        while ($row = $reader->getRow()) {
            echo "<tr>";
            foreach ($row as $col) {
                printf("<td>%s</td>", $col);
            }
            echo "</tr>";
        }
        echo "</table>";
    } catch (CSv_Exception_FileNotFound $e) {
        printf("<p>%s</p>", $e->getMessage());
Пример #6
0
 function testShouldFinishAtOriginalPositionAfterGettingHeader()
 {
     $reader = new Csv_Reader($this->files['tab-200']);
     $reader->setPosition(2);
     $reader->getHeader();
     $row = $reader->getRow();
     $this->assertEquals('Jermaine Chan', $row[0], 'should get specific row');
 }