blob: a217ad138cbcd2b9f2ef0963dc8b77ddf69d8d75 [file] [log] [blame]
admin1716d582006-09-24 20:15:03 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine79dc712006-09-26 03:52:45 +000015
16
17// INITIALIZE THE CLASS ---------------------------------------------------
18
19$obj =& get_instance();
admin7981a9a2006-09-26 07:52:09 +000020$obj->init_class('CI_DB_export', 'dbexport');
admine79dc712006-09-26 03:52:45 +000021
admin1716d582006-09-24 20:15:03 +000022// ------------------------------------------------------------------------
23
24/**
25 * DB Exporting Class
26 *
27 * @category Database
28 * @author Rick Ellis
29 * @link http://www.codeigniter.com/user_guide/database/
30 */
31class CI_DB_export {
admin910d8622006-09-26 02:09:05 +000032
33
adminf0839f32006-09-26 18:16:01 +000034 /**
35 * Constructor. Simply calls the log function
36 */
admin910d8622006-09-26 02:09:05 +000037 function CI_DB_export()
38 {
39 log_message('debug', "Database Export Class Initialized");
40 }
41
admin1716d582006-09-24 20:15:03 +000042 /**
adminb63ac842006-09-26 18:14:43 +000043 * Generate CVS from a query result object
admin1716d582006-09-24 20:15:03 +000044 *
45 * @access public
adminb63ac842006-09-26 18:14:43 +000046 * @param object The query result object
47 * @param string The delimiter - tab by default
48 * @param string The newline character - \n by default
49 * @return string
admin1716d582006-09-24 20:15:03 +000050 */
admin910d8622006-09-26 02:09:05 +000051 function generate_cvs($query, $delim = "\t", $newline = "\n")
admin1716d582006-09-24 20:15:03 +000052 {
admin910d8622006-09-26 02:09:05 +000053 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
54 {
55 show_error('You must submit a valid result object');
56 }
57
58 $out = '';
adminb63ac842006-09-26 18:14:43 +000059
60 // First generate the headings from the table column names
admin910d8622006-09-26 02:09:05 +000061 foreach ($query->field_names() as $name)
62 {
63 $out .= $name.$delim;
64 }
admin910d8622006-09-26 02:09:05 +000065 $out .= $newline;
66
adminb63ac842006-09-26 18:14:43 +000067 // Next blast through the result array and build out the rows
admin910d8622006-09-26 02:09:05 +000068 foreach ($query->result_array() as $row)
69 {
70 foreach ($row as $item)
71 {
72 $out .= $item.$delim;
73 }
74
75 $out .= $newline;
76 }
admin910d8622006-09-26 02:09:05 +000077
78 return $out;
admin1716d582006-09-24 20:15:03 +000079 }
80
81 // --------------------------------------------------------------------
82
admin910d8622006-09-26 02:09:05 +000083
admin1716d582006-09-24 20:15:03 +000084}
85
86?>