You are here

function pData::ImportFromCSV in Visitors 7.0

Same name and namespace in other branches
  1. 8 pchart/pData.inc \pData::ImportFromCSV()
  2. 7.2 pChart/class/pData.class.php \pData::importFromCSV()
  3. 7 pChart/class/pData.class.php \pData::importFromCSV()

File

pchart/pData.inc, line 63

Class

pData

Code

function ImportFromCSV($FileName, $Delimiter = ",", $DataColumns = -1, $HasHeader = FALSE, $DataName = -1) {
  $handle = @fopen($FileName, "r");
  if ($handle) {
    $HeaderParsed = FALSE;
    while (!feof($handle)) {
      $buffer = fgets($handle, 4096);
      $buffer = str_replace(chr(10), "", $buffer);
      $buffer = str_replace(chr(13), "", $buffer);
      $Values = split($Delimiter, $buffer);
      if ($buffer != "") {
        if ($HasHeader == TRUE && $HeaderParsed == FALSE) {
          if ($DataColumns == -1) {
            $ID = 1;
            foreach ($Values as $key => $Value) {
              $this
                ->SetSerieName($Value, "Serie" . $ID);
              $ID++;
            }
          }
          else {
            $SerieName = "";
            foreach ($DataColumns as $key => $Value) {
              $this
                ->SetSerieName($Values[$Value], "Serie" . $Value);
            }
          }
          $HeaderParsed = TRUE;
        }
        else {
          if ($DataColumns == -1) {
            $ID = 1;
            foreach ($Values as $key => $Value) {
              $this
                ->AddPoint(intval($Value), "Serie" . $ID);
              $ID++;
            }
          }
          else {
            $SerieName = "";
            if ($DataName != -1) {
              $SerieName = $Values[$DataName];
            }
            foreach ($DataColumns as $key => $Value) {
              $this
                ->AddPoint($Values[$Value], "Serie" . $Value, $SerieName);
            }
          }
        }
      }
    }
    fclose($handle);
  }
}