dokeos-library
[ class tree: dokeos-library ] [ index: dokeos-library ] [ all elements ]

Source for file import.lib.php

Documentation is available at import.lib.php

  1. <?php
  2. // $Id: import.lib.php 13806 2007-11-28 06:29:03Z yannoo $ 
  3. /*
  4. ============================================================================== 
  5.     Dokeos - elearning and course management software
  6.     
  7.     Copyright (c) 2004,2005 Dokeos S.A.
  8.     Copyright (c) Bart Mollet, Hogeschool Gent
  9.     
  10.     For a full list of contributors, see "credits.txt".
  11.     The full license can be read in "license.txt".
  12.     
  13.     This program is free software; you can redistribute it and/or
  14.     modify it under the terms of the GNU General Public License
  15.     as published by the Free Software Foundation; either version 2
  16.     of the License, or (at your option) any later version.
  17.     
  18.     See the GNU General Public License for more details.
  19.     
  20.     Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  21.     Mail: info@dokeos.com
  22. ============================================================================== 
  23. */
  24. /**
  25. ============================================================================== 
  26. * This class provides some functions which can be used when importing data from
  27. * external files into Dokeos
  28. @package     dokeos.library
  29. ============================================================================== 
  30. */
  31. class Import
  32. {
  33.     /**
  34.      * Reads a CSV-file into an array. The first line of the CSV-file should
  35.      * contain the array-keys.
  36.      * Example:
  37.      *   FirstName;LastName;Email
  38.      *   John;Doe;john.doe@mail.com
  39.      *   Adam;Adams;adam@mail.com
  40.      *  returns
  41.      *   $result [0]['FirstName'] = 'John';
  42.      *   $result [0]['LastName'] = 'Doe';
  43.      *   $result [0]['Email'] = 'john.doe@mail. com';
  44.      *   $result [1]['FirstName'] = 'Adam';
  45.      *   ...
  46.      * @param string $filename Path to the CSV-file which should be imported
  47.      * @return array An array with all data from the CSV-file
  48.      */
  49.     function csv_to_array($filename)
  50.     {
  51.         $result array ();
  52.         $handle fopen($filename"r");
  53.         if($handle === false)
  54.         {
  55.             return $result;
  56.         }
  57.         $keys fgetcsv($handle1000";");
  58.         while (($row_tmp fgetcsv($handle1000";")) !== FALSE)
  59.         {
  60.             
  61.             $row array ();
  62.             foreach ($row_tmp as $index => $value)
  63.             {
  64.                 $row[$keys[$index]] $value;
  65.             }
  66.             $result[$row;
  67.         }
  68.         fclose($handle);
  69.         return $result;
  70.     }
  71. }
  72. ?>

Documentation generated on Thu, 12 Jun 2008 13:47:11 -0500 by phpDocumentor 1.4.1