Login   Register  
PHP Classes
elePHPant
Icontem

File: csvwalk.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Martin A. Gutbrod  >  csvwalk  >  csvwalk.php  >  Download  
File: csvwalk.php
Role: ???
Content type: text/plain
Description: class+example
Class: csvwalk
help in reading csv-files
Author: By
Last change: fixed bug in Method next_row()
Date: 2002-06-19 21:45
Size: 2,922 bytes
 

Contents

Class file image Download
<?php
## 
## mag@massl.de  2002
##
## CLASS to walk with filter through a csv-file
## Name of fields are defined in first row.
##
/*    EXAMPLE

include_once("csvwalk.php");
$ccsv = new csvwalk();
if ($ccsv->open("../csvfiles/file.csv")) {   # if is's a path, take the newest file of type

 $ccsv->set_filter("Sachgebiet","^3\.");     # further filters are "and" combined

 while ($ccsv->next_row()) {
  echo "<br>".$ccsv->showfield("Sachgebiet");
  echo "<br>".$ccsv->showfield("Titel");
  echo "<br>".$ccsv->showfield("ID");
  echo "<br>".$ccsv->showfield("Beginn");
  echo "<br>".$ccsv->showfield("Ende");
  echo "<br>".$ccsv->showfield("Ort");
  echo "<br>".$ccsv->showfield("Betreuer");
  echo "<hr>";}

 $ccsv->close();}

EXAMPLE END 
*/


class csvwalk {

 var $fp;
 var $length;
 var $delimiter="";
 var $csvfile="";
 var $fields=array();
 var $filter=array();
 var $arow=array();
 var $irow=0;
 var $end=false;

 ## -- Constructor 
 function csvwalk($deli=";",$len=65536) {
  $this->length=$len;
  $this->delimiter=$deli;}

 ## -- open 
 function open($path) {
  if (is_dir($path)) {
   $this->newest_file($path,"\.csv$");}
  else {
   $this->csvfile=$path;}
  if ($this->fp=fopen($this->csvfile,"r")) {
   $this->fields=array_flip(fgetcsv($this->fp,$this->length,$this->delimiter));
   $this->arow=$this->fields;
   return true;}
  else {
   return false;}}

 ## -- Looking for newest file of $type in $path
 function newest_file($path,$type=".*") {
  clearstatcache();
  $path=ereg_replace("/$","",$path);
  if ($handle=opendir($path)) {
   $told=0;
   while (false!==($file=readdir($handle))) { 
    if ($file!="." && $file!=".." && ereg($type,$file) && !is_dir($path."/".$file)) {
     $tnew=filemtime($path."/".$file);
     if ($tnew>$told) {
      $told=$tnew;
      $this->csvfile=$path."/".$file;}}}
    closedir($handle);}}

 ## -- Set regex filter to field for reading
 function set_filter($field,$regex) {
  $this->filter[$field]=$regex;}

 ## -- check one row for filter conditions
 function check_filter(&$row) {
  foreach ($this->filter as $fld => $rex) {
   $v=$this->fields[$fld];
   if (!ereg($rex,$row[$v])) {
    return false;}}
  return true;}

 ## -- read next valid row
 function next_row() {
  $this->end=true;
  while ($adat=fgetcsv($this->fp,$this->length,$this->delimiter)) {
   if (count($adat) && $this->check_filter($adat)) {
    $this->irow++;
    $this->arow=$adat;
    $this->end=false;
    break;}}
  return !$this->end;}

 ## -- read field value of row (see example)
 function showfield($field) {
  $v=$this->fields[$field];
  return $this->arow[$v];}

 ## -- file-pointer to start of file; reset filter manually
 function reset() {
  $this->irow=0;
  $this->arow=array();
  rewind($this->fp);}

 ## -- finish!
 function close() {
  fclose ($this->fp);}

 }


?>