blob: 1704f0dfd5f54520ebb75bb6e0e29970471258fb [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
admin1716d582006-09-24 20:15:03 +000016// ------------------------------------------------------------------------
17
18/**
19 * DB Exporting Class
20 *
21 * @category Database
22 * @author Rick Ellis
23 * @link http://www.codeigniter.com/user_guide/database/
24 */
25class CI_DB_export {
admin910d8622006-09-26 02:09:05 +000026
27
adminf0839f32006-09-26 18:16:01 +000028 /**
29 * Constructor. Simply calls the log function
30 */
admin910d8622006-09-26 02:09:05 +000031 function CI_DB_export()
32 {
33 log_message('debug', "Database Export Class Initialized");
34 }
35
admineb13db72006-09-27 00:30:48 +000036 // --------------------------------------------------------------------
37
admin1716d582006-09-24 20:15:03 +000038 /**
adminb63ac842006-09-26 18:14:43 +000039 * Generate CVS from a query result object
admin1716d582006-09-24 20:15:03 +000040 *
41 * @access public
adminb63ac842006-09-26 18:14:43 +000042 * @param object The query result object
43 * @param string The delimiter - tab by default
44 * @param string The newline character - \n by default
45 * @return string
admin1716d582006-09-24 20:15:03 +000046 */
admineb13db72006-09-27 00:30:48 +000047 function cvs_from_result($query, $delim = "\t", $newline = "\n")
admin1716d582006-09-24 20:15:03 +000048 {
admin910d8622006-09-26 02:09:05 +000049 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
50 {
51 show_error('You must submit a valid result object');
52 }
53
54 $out = '';
adminb63ac842006-09-26 18:14:43 +000055
56 // First generate the headings from the table column names
admin910d8622006-09-26 02:09:05 +000057 foreach ($query->field_names() as $name)
58 {
59 $out .= $name.$delim;
60 }
admineb13db72006-09-27 00:30:48 +000061
62 $out = rtrim($out);
admin910d8622006-09-26 02:09:05 +000063 $out .= $newline;
64
adminb63ac842006-09-26 18:14:43 +000065 // Next blast through the result array and build out the rows
admin910d8622006-09-26 02:09:05 +000066 foreach ($query->result_array() as $row)
67 {
68 foreach ($row as $item)
69 {
70 $out .= $item.$delim;
71 }
admineb13db72006-09-27 00:30:48 +000072 $out = rtrim($out);
admin910d8622006-09-26 02:09:05 +000073 $out .= $newline;
74 }
admin910d8622006-09-26 02:09:05 +000075
76 return $out;
admin1716d582006-09-24 20:15:03 +000077 }
78
79 // --------------------------------------------------------------------
80
admin910d8622006-09-26 02:09:05 +000081
admineb13db72006-09-27 00:30:48 +000082 /**
83 * Generate XML data from a query result object
84 *
85 * @access public
86 * @param object The query result object
87 * @param array Any preferences
88 * @return string
89 */
90 function xml_from_result($query, $params = array())
91 {
92 if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
93 {
94 show_error('You must submit a valid result object');
95 }
96
97 // Set our default values
98 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
99 {
100 if ( ! isset($params[$key]))
101 {
102 $params[$key] = $val;
103 }
104 }
105
106 // Create variables for convenience
107 extract($params);
108
admin5abd0492006-09-27 01:54:01 +0000109 // Load the xml helper
110 $obj =& get_instance();
111 $obj->load->helper('xml');
112
admineb13db72006-09-27 00:30:48 +0000113 // Generate the result
admineb13db72006-09-27 00:30:48 +0000114 $xml = "<{$root}/>".$newline;
115 foreach ($query->result_array() as $row)
116 {
117 $xml .= $tab."<{$element}/>".$newline;
118
119 foreach ($row as $key => $val)
120 {
admin5abd0492006-09-27 01:54:01 +0000121 $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
admineb13db72006-09-27 00:30:48 +0000122 }
123 $xml .= $tab."</{$element}>".$newline;
124 }
125 $xml .= "</$root>".$newline;
126
127 return $xml;
128 }
129
admin1716d582006-09-24 20:15:03 +0000130}
131
132?>