1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @version V4.50 6 July 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved. |
---|
5 | * Released under both BSD license and Lesser GPL library license. |
---|
6 | * Whenever there is any discrepancy between the two licenses, |
---|
7 | * the BSD license will take precedence. |
---|
8 | * |
---|
9 | * Code to export recordsets in several formats: |
---|
10 | * |
---|
11 | * AS VARIABLE |
---|
12 | * $s = rs2csv($rs); # comma-separated values |
---|
13 | * $s = rs2tab($rs); # tab delimited |
---|
14 | * |
---|
15 | * TO A FILE |
---|
16 | * $f = fopen($path,'w'); |
---|
17 | * rs2csvfile($rs,$f); |
---|
18 | * fclose($f); |
---|
19 | * |
---|
20 | * TO STDOUT |
---|
21 | * rs2csvout($rs); |
---|
22 | */ |
---|
23 | |
---|
24 | // returns a recordset as a csv string |
---|
25 | function rs2csv(&$rs,$addtitles=true) |
---|
26 | { |
---|
27 | return _adodb_export($rs,',',',',false,$addtitles); |
---|
28 | } |
---|
29 | |
---|
30 | // writes recordset to csv file |
---|
31 | function rs2csvfile(&$rs,$fp,$addtitles=true) |
---|
32 | { |
---|
33 | _adodb_export($rs,',',',',$fp,$addtitles); |
---|
34 | } |
---|
35 | |
---|
36 | // write recordset as csv string to stdout |
---|
37 | function rs2csvout(&$rs,$addtitles=true) |
---|
38 | { |
---|
39 | $fp = fopen('php://stdout','wb'); |
---|
40 | _adodb_export($rs,',',',',true,$addtitles); |
---|
41 | fclose($fp); |
---|
42 | } |
---|
43 | |
---|
44 | function rs2tab(&$rs,$addtitles=true) |
---|
45 | { |
---|
46 | return _adodb_export($rs,"\t",',',false,$addtitles); |
---|
47 | } |
---|
48 | |
---|
49 | // to file pointer |
---|
50 | function rs2tabfile(&$rs,$fp,$addtitles=true) |
---|
51 | { |
---|
52 | _adodb_export($rs,"\t",',',$fp,$addtitles); |
---|
53 | } |
---|
54 | |
---|
55 | // to stdout |
---|
56 | function rs2tabout(&$rs,$addtitles=true) |
---|
57 | { |
---|
58 | $fp = fopen('php://stdout','wb'); |
---|
59 | _adodb_export($rs,"\t",' ',true,$addtitles); |
---|
60 | if ($fp) fclose($fp); |
---|
61 | } |
---|
62 | |
---|
63 | function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ') |
---|
64 | { |
---|
65 | if (!$rs) return ''; |
---|
66 | //---------- |
---|
67 | // CONSTANTS |
---|
68 | $NEWLINE = "\r\n"; |
---|
69 | $BUFLINES = 100; |
---|
70 | $escquotequote = $escquote.$quote; |
---|
71 | $s = ''; |
---|
72 | |
---|
73 | if ($addtitles) { |
---|
74 | $fieldTypes = $rs->FieldTypesArray(); |
---|
75 | reset($fieldTypes); |
---|
76 | while(list(,$o) = each($fieldTypes)) { |
---|
77 | |
---|
78 | $v = $o->name; |
---|
79 | if ($escquote) $v = str_replace($quote,$escquotequote,$v); |
---|
80 | $v = strip_tags(str_replace("\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))); |
---|
81 | $elements[] = $v; |
---|
82 | |
---|
83 | } |
---|
84 | $s .= implode($sep, $elements).$NEWLINE; |
---|
85 | } |
---|
86 | $hasNumIndex = isset($rs->fields[0]); |
---|
87 | |
---|
88 | $line = 0; |
---|
89 | $max = $rs->FieldCount(); |
---|
90 | |
---|
91 | while (!$rs->EOF) { |
---|
92 | $elements = array(); |
---|
93 | $i = 0; |
---|
94 | |
---|
95 | if ($hasNumIndex) { |
---|
96 | for ($j=0; $j < $max; $j++) { |
---|
97 | $v = trim($rs->fields[$j]); |
---|
98 | if ($escquote) $v = str_replace($quote,$escquotequote,$v); |
---|
99 | $v = strip_tags(str_replace("\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))); |
---|
100 | |
---|
101 | if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote"; |
---|
102 | else $elements[] = $v; |
---|
103 | } |
---|
104 | } else { // ASSOCIATIVE ARRAY |
---|
105 | foreach($rs->fields as $v) { |
---|
106 | if ($escquote) $v = str_replace($quote,$escquotequote,trim($v)); |
---|
107 | $v = strip_tags(str_replace("\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))); |
---|
108 | |
---|
109 | if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote"; |
---|
110 | else $elements[] = $v; |
---|
111 | } |
---|
112 | } |
---|
113 | $s .= implode($sep, $elements).$NEWLINE; |
---|
114 | $rs->MoveNext(); |
---|
115 | $line += 1; |
---|
116 | if ($fp && ($line % $BUFLINES) == 0) { |
---|
117 | if ($fp === true) echo $s; |
---|
118 | else fwrite($fp,$s); |
---|
119 | $s = ''; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | if ($fp) { |
---|
124 | if ($fp === true) echo $s; |
---|
125 | else fwrite($fp,$s); |
---|
126 | $s = ''; |
---|
127 | } |
---|
128 | |
---|
129 | return $s; |
---|
130 | } |
---|
131 | ?> |
---|