/** * Проверяем структуру присланного файла * @return bool true если структура верна */ private function _testStructure() { if (!$this->_csvDataSource->isSymmetric()) { return false; } $wantedHeaders = array('ReceiptDate', 'TransactionType', 'Account', 'Value', 'Currency', 'Category', 'Comment'); $intersection = array_intersect($wantedHeaders, $this->_csvDataSource->getHeaders()); if (count($wantedHeaders) != count($intersection)) { return false; } return true; }
protected function _getCSV() { $csv = new File_CSV_DataSource(); $csv->settings = $this->_csv_settings; if (!$csv->load($this->_file)) { print "\nERROR: file {$this->_file} not found!"; } // Header Namen matchen - Ziel: technische Namen behalten, Beschreibung entfernen $headers = $csv->getHeaders(); //print_r($headers); foreach ($headers as $k => $v) { $tmp = $v; if (preg_match('/\\[(.*)\\]"?$/', $tmp, $match)) { $tmp = preg_replace('/[^\\w\\d\\/\\.]/i', '', $match[1]); } $this->_headers[$k] = $tmp; } //print "\nHeaders: "; //print_r($this->_headers); return $csv; }
<?php require_once "DataSource.php"; $url = !empty($_REQUEST['url']) ? $_REQUEST['url'] : 'tests/data/names.csv'; $DEBUG = false; $csv = new File_CSV_DataSource($url); //Headers $headers = $csv->getHeaders(); // array('name', 'age'); $table = "<table border=1><tr>"; foreach ($headers as $h) { $table .= '<th style="background:#ccc">' . $h . "</th>"; } $table .= "</tr>"; //Columns $csv_array = $csv->connect(); if ($DEBUG) { print_r($csv_array); } foreach ($csv_array as $csv) { $table .= "<tr><td>" . $csv['name'] . "</td><td>" . $csv['age'] . "</td></tr>"; } $table .= "</table>"; echo $table;
<?php require_once 'DataSource.php'; $csv = new File_CSV_DataSource('tests/data/products.csv'); $html .= '<table>'; $html .= '<tr>'; foreach ($csv->getHeaders() as $header) { $html .= '<th>' . $header . '</th>'; } $html .= '</tr>'; foreach ($csv->connect() as $row) { $html .= '<tr>'; foreach ($row as $key => $value) { $html .= '<td>' . $value . '</td>'; } $html .= '</tr>'; } $html .= '</table>'; echo $html;