function pData::importFromCSV

5.x pData.class.php pData::importFromCSV($FileName, $Options = "")

File

inc/pchart/pchart/class/pData.class.php, line 667

Class

pData

Code

function importFromCSV($FileName, $Options = "") 
 {
  $Delimiter = isset($Options ["Delimiter"]) ? $Options ["Delimiter"] : ",";
  $GotHeader = isset($Options ["GotHeader"]) ? $Options ["GotHeader"] : FALSE;
  $SkipColumns = isset($Options ["SkipColumns"]) ? $Options ["SkipColumns"] : array(-1);
  $DefaultSerieName = isset($Options ["DefaultSerieName"]) ? $Options ["DefaultSerieName"] : "Serie";

  $Handle = @fopen($FileName, "r");
  if ($Handle) 
   {
    $HeaderParsed = FALSE;
    $SerieNames = "";
    while (!feof($Handle)) 
     {
      $Buffer = fgets($Handle, 4096);
      $Buffer = str_replace(chr(10), "", $Buffer);
      $Buffer = str_replace(chr(13), "", $Buffer);
      $Values = preg_split("/" . $Delimiter . "/", $Buffer);

      if ($Buffer != "") 
       {
        if ($GotHeader && !$HeaderParsed) 
         {
          foreach ($Values as $Key => $Name) {
            if (!in_array($Key, $SkipColumns)) {
              $SerieNames [$Key] = $Name;
            }
          }
          $HeaderParsed = TRUE;
        }
        else 
         {
          if ($SerieNames == "") {
            foreach ($Values as $Key => $Name) {
              if (!in_array($Key, $SkipColumns)) {
                $SerieNames [$Key] = $DefaultSerieName . $Key;
              }
            }
          }
          foreach ($Values as $Key => $Value) {
            if (!in_array($Key, $SkipColumns)) {
              $this->addPoints($Value, $SerieNames [$Key]);
            }
          }
        }
      }
    }
    fclose($Handle);
  }
}