Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   1  <?php
   2  /**
   3   * Export recordsets in several formats.
   4   *
   5   * AS VARIABLE
   6   * $s = rs2csv($rs); # comma-separated values
   7   * $s = rs2tab($rs); # tab delimited
   8   *
   9   * TO A FILE
  10   * $f = fopen($path,'w');
  11   * rs2csvfile($rs,$f);
  12   * fclose($f);
  13   *
  14   * TO STDOUT
  15   * rs2csvout($rs);
  16   *
  17   * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
  18   *
  19   * @package ADOdb
  20   * @link https://adodb.org Project's web site and documentation
  21   * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
  22   *
  23   * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
  24   * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
  25   * any later version. This means you can use it in proprietary products.
  26   * See the LICENSE.md file distributed with this source code for details.
  27   * @license BSD-3-Clause
  28   * @license LGPL-2.1-or-later
  29   *
  30   * @copyright 2000-2013 John Lim
  31   * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
  32   */
  33  
  34  // returns a recordset as a csv string
  35  function rs2csv(&$rs,$addtitles=true)
  36  {
  37  	 return _adodb_export($rs,',',',',false,$addtitles);
  38  }
  39  
  40  // writes recordset to csv file
  41  function rs2csvfile(&$rs,$fp,$addtitles=true)
  42  {
  43  	 _adodb_export($rs,',',',',$fp,$addtitles);
  44  }
  45  
  46  // write recordset as csv string to stdout
  47  function rs2csvout(&$rs,$addtitles=true)
  48  {
  49  	 $fp = fopen('php://stdout','wb');
  50  	 _adodb_export($rs,',',',',true,$addtitles);
  51  	 fclose($fp);
  52  }
  53  
  54  function rs2tab(&$rs,$addtitles=true)
  55  {
  56  	 return _adodb_export($rs,"\t",',',false,$addtitles);
  57  }
  58  
  59  // to file pointer
  60  function rs2tabfile(&$rs,$fp,$addtitles=true)
  61  {
  62  	 _adodb_export($rs,"\t",',',$fp,$addtitles);
  63  }
  64  
  65  // to stdout
  66  function rs2tabout(&$rs,$addtitles=true)
  67  {
  68  	 $fp = fopen('php://stdout','wb');
  69  	 _adodb_export($rs,"\t",' ',true,$addtitles);
  70  	 if ($fp) fclose($fp);
  71  }
  72  
  73  function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
  74  {
  75  	 if (!$rs) return '';
  76  	 //----------
  77  	 // CONSTANTS
  78  	 $NEWLINE = "\r\n";
  79  	 $BUFLINES = 100;
  80  	 $escquotequote = $escquote.$quote;
  81  	 $s = '';
  82  
  83  	 if ($addtitles) {
  84  	 	 $fieldTypes = $rs->FieldTypesArray();
  85  	 	 reset($fieldTypes);
  86  	 	 $i = 0;
  87  	 	 $elements = array();
  88  	 	 foreach ($fieldTypes as $o) {
  89  
  90  	 	 	 $v = ($o) ? $o->name : 'Field'.($i++);
  91  	 	 	 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
  92  	 	 	 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
  93  	 	 	 $elements[] = $v;
  94  
  95  	 	 }
  96  	 	 $s .= implode($sep, $elements).$NEWLINE;
  97  	 }
  98  	 $hasNumIndex = isset($rs->fields[0]);
  99  
 100  	 $line = 0;
 101  	 $max = $rs->FieldCount();
 102  
 103  	 while (!$rs->EOF) {
 104  	 	 $elements = array();
 105  	 	 $i = 0;
 106  
 107  	 	 if ($hasNumIndex) {
 108  	 	 	 for ($j=0; $j < $max; $j++) {
 109  	 	 	 	 $v = $rs->fields[$j];
 110  	 	 	 	 if (!is_object($v)) $v = trim($v);
 111  	 	 	 	 else $v = 'Object';
 112  	 	 	 	 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
 113  	 	 	 	 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
 114  
 115  	 	 	 	 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
 116  	 	 	 	 else $elements[] = $v;
 117  	 	 	 }
 118  	 	 } else { // ASSOCIATIVE ARRAY
 119  	 	 	 foreach($rs->fields as $v) {
 120  	 	 	 	 if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
 121  	 	 	 	 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
 122  
 123  	 	 	 	 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
 124  	 	 	 	 else $elements[] = $v;
 125  	 	 	 }
 126  	 	 }
 127  	 	 $s .= implode($sep, $elements).$NEWLINE;
 128  	 	 $rs->MoveNext();
 129  	 	 $line += 1;
 130  	 	 if ($fp && ($line % $BUFLINES) == 0) {
 131  	 	 	 if ($fp === true) echo $s;
 132  	 	 	 else fwrite($fp,$s);
 133  	 	 	 $s = '';
 134  	 	 }
 135  	 }
 136  
 137  	 if ($fp) {
 138  	 	 if ($fp === true) echo $s;
 139  	 	 else fwrite($fp,$s);
 140  	 	 $s = '';
 141  	 }
 142  
 143  	 return $s;
 144  }