2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
16 // var $quote_search = array('"', "\n", "\r", "\x00");
17 // var $quote_replace = array('""', '\n', '\r', '\0');
18 var $quote_search = array('"', "\n", "\r", "\x00");
19 var $quote_replace = array('""', '\n', '\r', '\0');
22 function CSVExport() { }
25 function export($sql, $course_id) {
27 $sql = str_replace('?', $course_id , $sql);
31 $result = mysql_query($sql, $db);
33 $field_types = $this->detectFieldTypes($result);
37 $num_fields = count($field_types);
39 while ($row = mysql_fetch_row($result)) {
40 for ($i=0; $i < $num_fields; $i++) {
41 if ($types[$i] == 'int' || $types[$i] == 'real') {
42 $content .= $row[$i] . ',';
44 $content .= $this->quote($row[$i]) . ',';
47 $content = substr($content, 0, -1);
51 @mysql_free_result($result);
57 // given a query result returns an array of field types.
58 // possible field types are int, string, datetime, or blob...
59 function detectFieldTypes(&$result) {
60 $field_types = array();
61 $num_fields = @mysql_num_fields($result);
67 for ($i=0; $i< $num_fields; $i++) {
68 $field_types[] = mysql_field_type($result, $i);
74 // quote $line so that it's safe to save as a CSV field
75 function quote($line) {
76 return '"'.str_replace($this->quote_search, $this->quote_replace, $line).'"';