<?php
/*
    CREATIVE COMMONS - Attribution-No Derivative Works 3.0 Germany:
    - You may not alter, transform, or build upon this work.
    - Dieses Werk darf nicht bearbeitet oder in anderer Weise verändert werden.
*/
global $db, $prefix, $exportDirectory;

$datei = $exportDirectory.'/'.$Table.'.csv';
$fp=fopen($datei,"w+");
// Header definieren
  $Spalten = array ();
  $Header = array ();

foreach ($Array as $key => $value) {
  if ($value[1] == 1) {
      $Spalten[] = $key;
      $Header[] = optimizeString($value[0]); }}

$String = implode(getSetup('SetupExportSeparator'),$Header) . "\n";
if (!fwrite($fp,$String))
  echo '<span class="ErrorMessage">ERROR: Can NOT write the file! ('.$Table.'.csv)</span><br />';
else {
  // Daten ausgeben
  $SQLQuery = "SELECT * FROM ".$prefix.$Table;
  $Result = mysqli_query($db, $SQLQuery);
  while ($row = mysqli_fetch_array($Result)) {
    extract($row);
    $Zeile = array ();
    foreach ($Spalten as $key) {
      $pos = $$key;
      if ($Array[$key][2] == 1) {
        $pos = optimizeString($pos); }
      else {
        include('functions/export/optimize_' . $Table . '.php'); }
      $Zeile[] = $pos; }
    $String = implode(getSetup('SetupExportSeparator'),$Zeile) . "\n";
    fwrite($fp,$String); }}
fclose ($fp);