From eb13db750ac04921774b8bc7d8b26f343195061a Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 27 Sep 2006 00:30:48 +0000 Subject: --- system/database/DB_export.php | 80 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/system/database/DB_export.php b/system/database/DB_export.php index a217ad138..8d6446cb6 100644 --- a/system/database/DB_export.php +++ b/system/database/DB_export.php @@ -39,6 +39,8 @@ class CI_DB_export { log_message('debug', "Database Export Class Initialized"); } + // -------------------------------------------------------------------- + /** * Generate CVS from a query result object * @@ -48,7 +50,7 @@ class CI_DB_export { * @param string The newline character - \n by default * @return string */ - function generate_cvs($query, $delim = "\t", $newline = "\n") + function cvs_from_result($query, $delim = "\t", $newline = "\n") { if ( ! is_object($query) OR ! method_exists($query, 'field_names')) { @@ -62,6 +64,8 @@ class CI_DB_export { { $out .= $name.$delim; } + + $out = rtrim($out); $out .= $newline; // Next blast through the result array and build out the rows @@ -71,7 +75,7 @@ class CI_DB_export { { $out .= $item.$delim; } - + $out = rtrim($out); $out .= $newline; } @@ -81,6 +85,78 @@ class CI_DB_export { // -------------------------------------------------------------------- + /** + * Generate XML data from a query result object + * + * @access public + * @param object The query result object + * @param array Any preferences + * @return string + */ + function xml_from_result($query, $params = array()) + { + if ( ! is_object($query) OR ! method_exists($query, 'field_names')) + { + show_error('You must submit a valid result object'); + } + + // Set our default values + foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val) + { + if ( ! isset($params[$key])) + { + $params[$key] = $val; + } + } + + // Create variables for convenience + extract($params); + + // Generate the result + + $xml = "<{$root}/>".$newline; + foreach ($query->result_array() as $row) + { + $xml .= $tab."<{$element}/>".$newline; + + foreach ($row as $key => $val) + { + $xml .= $tab.$tab."<{$key}>".$this->_xml_convert($val)."".$newline; + } + $xml .= $tab."".$newline; + } + $xml .= "".$newline; + + return $xml; + } + + + // ------------------------------------------------------------------------ + + /** + * Convert Reserved XML characters to Entities + * + * @access public + * @param string + * @return string + */ + function _xml_convert($str) + { + $temp = '__TEMP_AMPERSANDS'; + + $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str); + $str = preg_replace("/&(\w+);/", "$temp\\1;", $str); + + $str = str_replace(array("&","<",">","\"", "'", "-"), + array("&", "<", ">", """, "'", "-"), + $str); + + $str = preg_replace("/$temp(\d+);/","&#\\1;",$str); + $str = preg_replace("/$temp(\w+);/","&\\1;", $str); + + return $str; + } + } ?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b