2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
15 // var $quote_search = array('"', "\n", "\r", "\x00");
16 // var $quote_replace = array('""', '\n', '\r', '\0');
17 var $quote_search = array('"', "\n", "\r", "\x00");
18 var $quote_replace = array('""', '\n', '\r', '\0');
21 function CSVExport() { }
24 function export($sql, $course_id) {
26 $sql = str_replace('?', $course_id , $sql);
30 $result = mysql_query($sql, $db);
32 $field_types = $this->detectFieldTypes($result);
36 $num_fields = count($field_types);
38 while ($row = mysql_fetch_row($result)) {
39 for ($i=0; $i < $num_fields; $i++) {
40 if ($types[$i] == 'int' || $types[$i] == 'real') {
41 $content .= $row[$i] . ',';
43 $content .= $this->quote($row[$i]) . ',';
46 $content = substr($content, 0, -1);
50 @mysql_free_result($result);
56 // given a query result returns an array of field types.
57 // possible field types are int, string, datetime, or blob...
58 function detectFieldTypes(&$result) {
59 $field_types = array();
60 $num_fields = @mysql_num_fields($result);
66 for ($i=0; $i< $num_fields; $i++) {
67 $field_types[] = mysql_field_type($result, $i);
73 // quote $line so that it's safe to save as a CSV field
74 function quote($line) {
75 return '"'.str_replace($this->quote_search, $this->quote_replace, $line).'"';