<?php


class Csvexport
{
    /* Header: Array of field descriptions */

    var $fields = Array();
    /* Data rows (array of arrays) */
    var $data = Array();


    function __construct($filename, $fieldnames, $data)
    {
        $this->filename = $filename;
        $this->fields = $fieldnames;
        $this->data = $data;
    }


    function create()
    {
        print "Schreibe " . $this->filename . "<br/>";
        $fout = fopen($this->filename, 'w');
        if ($fout == null) {
            error("Kann '" . $this->filename . "' nicht zum schreiben öffnen");
        }
        $first = true;
        foreach ($this->fields as $fieldname)
        {
            if ($first) {
                $first = false;
            }
            else {
                fwrite($fout, ';');
            }
            fwrite($fout, $fieldname);
        }
        fwrite($fout, "\n");
        foreach ($this->data as $row)
        {
            $first = true;
            foreach ($row as $field)
            {
                if ($first) {
                    $first = false;
                }
                else {
                    fwrite($fout, ';');
                }
                fwrite($fout, '"'.$field.'"');
            }
            fwrite($fout, "\n");
        }
        fclose($fout);
    }

}
