Hallo,
Es gibt ein paar vorgefertigte Funktionen dafuer, wie fgetcsv/fputcsv, die es einem etwas einfacher machen, CSV-Dateien einzulesen bzw. CSV-Datensaetze wegzuschreiben.
Ich habe auch mal einige Klassen zum Thema CSV geschrieben. es gibt zwar keine Doku, aber ich poste sie mal und fragen beantworte ich gerne:
Die Vorteile dieser Klasse:
- Du kannst (theoretisch) das Model austauschen gegen z.B. eine Datenbank, andere Klassen als CSV gibt es im Moment noch nicht
- Die Tabelle hat ein automatisched ID-Feld, es wird z.B. auf Wunsch automatisch überschrieben.
Wenn du die Klassen erweiterst / neue Klassen erstellst, fände ich es schön, informiert zu werden.
abstract class Model {
protected $data;
protected $dimensions = 1;
public function __construct($dimensions) {
$this->dimensions = $dimensions;
}
public function __destruct() {}
abstract public function save();
abstract public function open();
}
abstract class Model2D extends Model {
protected $idfield = -1;
protected $rowlength = 1;
public function __construct($rowlength) {
parent::__construct(2);
$this->rowlength = $rowlength;
}
public function __destruct() {
parent::__destruct();
unset($data);
}
public function setIdfield($idfield) {
if(is_numeric($idfield)) {
if($idfield < $this->rowlength) {
$this->idfield = $idfield;
} else {
throw new Exception('idfield must be lower than rowlength');
}
} else {
throw new Exception('idfield has to be numeric');
}
}
public function getIdfield() {
return $this->idfield;
}
public function add($row) {
if($this->idfield != -1) {
if($this->idExists($row[$this->idfield])) {
throw new Exception('A row with this ID exists');
return;
}
}
if(count($row) > $this->rowlength) {
$row = array_slice($row,0,$this->rowlength);
} else if(count($row) < $this->rowlength) {
$row = array_pad($row, $this->rowlength, '');
}
$this->data[] = $row;
}
public function remove($id) {
$key = $this->findRow($id);
if($key != -1) {
unset($this->data[$key]);
}
}
public function idExists($id, $idfield = -1) {
if($idfield == -1) {
if($this->idfield != -1) {
$idfield = $this->idfield;
} else {
$idfield = 0;
}
}
foreach($this->data as $key => $row) {
if($row[$idfield] == $id) {
return true;
}
}
return false;
}
public function update($id, $data, $idfield = -1) {
if($idfield == -1) {
if($this->idfield != -1) {
$idfield = $this->idfield;
} else {
$idfield = 0;
}
}
foreach($this->data as $key => $row) {
if($row[$idfield] == $id) {
if(count($data) > $this->rowlength) {
$data = array_slice($data,0,$this->rowlength);
} else if(count($data) < $this->rowlength) {
$data = array_pad($data, $this->rowlength, '');
}
$this->data[$key] = $data;
return $key;
}
}
return -1;
}
public function findRow($id, $idfield = -1) {
if($idfield == -1) {
if($this->idfield != -1) {
$idfield = $this->idfield;
} else {
$idfield = 0;
}
}
foreach($this->data as $key => $row) {
if($row[$idfield] == $id) {
return $key;
}
}
return -1;
}
}
class CSV extends Model2D {
private $handle;
private $delimiter = ",";
private $flcaption = false;
private $fields;
public function __construct($filename, $rowlength) {
parent::__construct($rowlength);
if(file_exists($filename) && !is_dir($filename)) {
$this->handle = fopen($filename, "a+");
}
}
public function save() {
ftruncate($this->handle, 0);
rewind($this->handle);
if($this->flcaption) {
fwrite($this->handle, CSV::toCsvString($this->fields, $this->delimiter)."\r\n");
}
foreach($this->data as $row) {
fwrite($this->handle, CSV::toCsvString($row, $this->delimiter)."\r\n");
}
}
public function setDelimiter($delimiter) {
$this->delimiter = $delimiter{0};
}
public function setFirstLineCaption($flcaption) {
$this->flcaption = ($flcaption == true);
}
public function setCaptions($fields) {
if(is_array($fields)) {
$this->fields = $fields;
} else {
throw new Exception('fields has to be an array');
}
}
public function getDelimiter() {
return $this->delimiter;
}
public function getFirstLineCaption() {
return $this->flcaption;
}
public function getCaptions() {
return $this->fields;
}
public function open() {
rewind($this->handle);
if($this->flcaption) {
$line = fgets($this->handle, 65526);
$line = str_replace(chr(10), '', $line);
$line = str_replace(chr(13), '', $line);
$row = explode($this->delimiter, $line);
$this->fields = $row;
}
while(!feof($this->handle)) {
$line = fgets($this->handle, 65526);
if(trim($line) == "") continue;
$line = str_replace(chr(10), '', $line);
$line = str_replace(chr(13), '', $line);
$row = explode($this->delimiter, $line);
$this->data[] = $row;
}
}
public function set($id, $data) {
foreach($this->data as $key=>$row) {
if($row[$this->idfield] == $id) {
$this->data[$key] = $data;
return true;
}
}
return false;
}
public function __destruct() {
fclose($this->handle);
parent::__destruct();
}
public static function toCsvString($data, $delimiter) {
return implode($delimiter, $data);
}
public function asAssoziative() {
$a = array();
foreach($this->data as $key => $value) {
if($this->idfield == -1) {
$a[$key] = $this->makeFieldNamesAsKeys($value);
} else {
$a[$value[$this->idfield]] = $this->makeFieldNamesAsKeys($value);
}
}
return $a;
}
protected function makeFieldNamesAsKeys($values) {
$return = array();
foreach($values as $k => $v) {
$return[$this->fields[$k]] = $v;
}
return $return;
}
}
mfg, Flo
--
sh:) fo:| ch:? rl:( br:^ n4:| ie:{ mo:| va:} de:> zu:} fl:{ ss:) ls:< js:|
sh:) fo:| ch:? rl:( br:^ n4:| ie:{ mo:| va:} de:> zu:} fl:{ ss:) ls:< js:|