Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/adodb/toexport.inc.php
Go to the documentation of this file.
00001 <?php
00002 
00024 // returns a recordset as a csv string
00025 function rs2csv(&$rs,$addtitles=true)
00026 {
00027         return _adodb_export($rs,',',',',false,$addtitles);
00028 }
00029 
00030 // writes recordset to csv file 
00031 function rs2csvfile(&$rs,$fp,$addtitles=true)
00032 {
00033         _adodb_export($rs,',',',',$fp,$addtitles);
00034 }
00035 
00036 // write recordset as csv string to stdout
00037 function rs2csvout(&$rs,$addtitles=true)
00038 {
00039         $fp = fopen('php://stdout','wb');
00040         _adodb_export($rs,',',',',true,$addtitles);
00041         fclose($fp);
00042 }
00043 
00044 function rs2tab(&$rs,$addtitles=true)
00045 {
00046         return _adodb_export($rs,"\t",',',false,$addtitles);
00047 }
00048 
00049 // to file pointer
00050 function rs2tabfile(&$rs,$fp,$addtitles=true)
00051 {
00052         _adodb_export($rs,"\t",',',$fp,$addtitles);
00053 }
00054 
00055 // to stdout
00056 function rs2tabout(&$rs,$addtitles=true)
00057 {
00058         $fp = fopen('php://stdout','wb');
00059         _adodb_export($rs,"\t",' ',true,$addtitles);
00060         if ($fp) fclose($fp);
00061 }
00062 
00063 function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
00064 {
00065         if (!$rs) return '';
00066         //----------
00067         // CONSTANTS
00068         $NEWLINE = "\r\n";
00069         $BUFLINES = 100;
00070         $escquotequote = $escquote.$quote;
00071         $s = '';
00072         
00073         if ($addtitles) {
00074                 $fieldTypes = $rs->FieldTypesArray();
00075                 reset($fieldTypes);
00076                 $i = 0;
00077                 while(list(,$o) = each($fieldTypes)) {
00078                 
00079                         $v = ($o) ? $o->name : 'Field'.($i++);
00080                         if ($escquote) $v = str_replace($quote,$escquotequote,$v);
00081                         $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
00082                         $elements[] = $v;
00083                         
00084                 }
00085                 $s .= implode($sep, $elements).$NEWLINE;
00086         }
00087         $hasNumIndex = isset($rs->fields[0]);
00088         
00089         $line = 0;
00090         $max = $rs->FieldCount();
00091         
00092         while (!$rs->EOF) {
00093                 $elements = array();
00094                 $i = 0;
00095                 
00096                 if ($hasNumIndex) {
00097                         for ($j=0; $j < $max; $j++) {
00098                                 $v = $rs->fields[$j];
00099                                 if (!is_object($v)) $v = trim($v);
00100                                 else $v = 'Object';
00101                                 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
00102                                 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
00103                                 
00104                                 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
00105                                 else $elements[] = $v;
00106                         }
00107                 } else { // ASSOCIATIVE ARRAY
00108                         foreach($rs->fields as $v) {
00109                                 if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
00110                                 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
00111                                 
00112                                 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
00113                                 else $elements[] = $v;
00114                         }
00115                 }
00116                 $s .= implode($sep, $elements).$NEWLINE;
00117                 $rs->MoveNext();
00118                 $line += 1;
00119                 if ($fp && ($line % $BUFLINES) == 0) {
00120                         if ($fp === true) echo $s;
00121                         else fwrite($fp,$s);
00122                         $s = '';
00123                 }
00124         }
00125         
00126         if ($fp) {
00127                 if ($fp === true) echo $s;
00128                 else fwrite($fp,$s);
00129                 $s = '';
00130         }
00131         
00132         return $s;
00133 }
00134 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations