blob: a2e3e3e4cc0fc3ffbec440d10bad86745bb06a01 [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
admineb13db72006-09-27 00:30:48 +000042 // --------------------------------------------------------------------
43
admin1716d582006-09-24 20:15:03 +000044 /**
adminb63ac842006-09-26 18:14:43 +000045 * Generate CVS from a query result object
admin1716d582006-09-24 20:15:03 +000046 *
47 * @access public
adminb63ac842006-09-26 18:14:43 +000048 * @param object The query result object
49 * @param string The delimiter - tab by default
50 * @param string The newline character - \n by default
51 * @return string
admin1716d582006-09-24 20:15:03 +000052 */
admineb13db72006-09-27 00:30:48 +000053 function cvs_from_result($query, $delim = "\t", $newline = "\n")
admin1716d582006-09-24 20:15:03 +000054 {
admin910d8622006-09-26 02:09:05 +000055 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
56 {
57 show_error('You must submit a valid result object');
58 }
59
60 $out = '';
adminb63ac842006-09-26 18:14:43 +000061
62 // First generate the headings from the table column names
admin910d8622006-09-26 02:09:05 +000063 foreach ($query->field_names() as $name)
64 {
65 $out .= $name.$delim;
66 }
admineb13db72006-09-27 00:30:48 +000067
68 $out = rtrim($out);
admin910d8622006-09-26 02:09:05 +000069 $out .= $newline;
70
adminb63ac842006-09-26 18:14:43 +000071 // Next blast through the result array and build out the rows
admin910d8622006-09-26 02:09:05 +000072 foreach ($query->result_array() as $row)
73 {
74 foreach ($row as $item)
75 {
76 $out .= $item.$delim;
77 }
admineb13db72006-09-27 00:30:48 +000078 $out = rtrim($out);
admin910d8622006-09-26 02:09:05 +000079 $out .= $newline;
80 }
admin910d8622006-09-26 02:09:05 +000081
82 return $out;
admin1716d582006-09-24 20:15:03 +000083 }
84
85 // --------------------------------------------------------------------
86
admin910d8622006-09-26 02:09:05 +000087
admineb13db72006-09-27 00:30:48 +000088 /**
89 * Generate XML data from a query result object
90 *
91 * @access public
92 * @param object The query result object
93 * @param array Any preferences
94 * @return string
95 */
96 function xml_from_result($query, $params = array())
97 {
98 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
99 {
100 show_error('You must submit a valid result object');
101 }
102
103 // Set our default values
104 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
105 {
106 if ( ! isset($params[$key]))
107 {
108 $params[$key] = $val;
109 }
110 }
111
112 // Create variables for convenience
113 extract($params);
114
admin5abd0492006-09-27 01:54:01 +0000115 // Load the xml helper
116 $obj =& get_instance();
117 $obj->load->helper('xml');
118
admineb13db72006-09-27 00:30:48 +0000119 // Generate the result
admineb13db72006-09-27 00:30:48 +0000120 $xml = "<{$root}/>".$newline;
121 foreach ($query->result_array() as $row)
122 {
123 $xml .= $tab."<{$element}/>".$newline;
124
125 foreach ($row as $key => $val)
126 {
admin5abd0492006-09-27 01:54:01 +0000127 $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
admineb13db72006-09-27 00:30:48 +0000128 }
129 $xml .= $tab."</{$element}>".$newline;
130 }
131 $xml .= "</$root>".$newline;
132
133 return $xml;
134 }
135
admin1716d582006-09-24 20:15:03 +0000136}
137
138?>