admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 1 | <?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 | */ |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | // INITIALIZE THE CLASS --------------------------------------------------- |
| 18 | |
| 19 | $obj =& get_instance(); |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 20 | $obj->init_class('CI_DB_export', 'dbexport'); |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 21 | |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 22 | // ------------------------------------------------------------------------ |
| 23 | |
| 24 | /** |
| 25 | * DB Exporting Class |
| 26 | * |
| 27 | * @category Database |
| 28 | * @author Rick Ellis |
| 29 | * @link http://www.codeigniter.com/user_guide/database/ |
| 30 | */ |
| 31 | class CI_DB_export { |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 32 | |
| 33 | |
admin | f0839f3 | 2006-09-26 18:16:01 +0000 | [diff] [blame] | 34 | /** |
| 35 | * Constructor. Simply calls the log function |
| 36 | */ |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 37 | function CI_DB_export() |
| 38 | { |
| 39 | log_message('debug', "Database Export Class Initialized"); |
| 40 | } |
| 41 | |
admin | eb13db7 | 2006-09-27 00:30:48 +0000 | [diff] [blame] | 42 | // -------------------------------------------------------------------- |
| 43 | |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 44 | /** |
admin | b63ac84 | 2006-09-26 18:14:43 +0000 | [diff] [blame] | 45 | * Generate CVS from a query result object |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 46 | * |
| 47 | * @access public |
admin | b63ac84 | 2006-09-26 18:14:43 +0000 | [diff] [blame] | 48 | * @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 |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 52 | */ |
admin | eb13db7 | 2006-09-27 00:30:48 +0000 | [diff] [blame] | 53 | function cvs_from_result($query, $delim = "\t", $newline = "\n") |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 54 | { |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 55 | 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 = ''; |
admin | b63ac84 | 2006-09-26 18:14:43 +0000 | [diff] [blame] | 61 | |
| 62 | // First generate the headings from the table column names |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 63 | foreach ($query->field_names() as $name) |
| 64 | { |
| 65 | $out .= $name.$delim; |
| 66 | } |
admin | eb13db7 | 2006-09-27 00:30:48 +0000 | [diff] [blame] | 67 | |
| 68 | $out = rtrim($out); |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 69 | $out .= $newline; |
| 70 | |
admin | b63ac84 | 2006-09-26 18:14:43 +0000 | [diff] [blame] | 71 | // Next blast through the result array and build out the rows |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 72 | foreach ($query->result_array() as $row) |
| 73 | { |
| 74 | foreach ($row as $item) |
| 75 | { |
| 76 | $out .= $item.$delim; |
| 77 | } |
admin | eb13db7 | 2006-09-27 00:30:48 +0000 | [diff] [blame] | 78 | $out = rtrim($out); |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 79 | $out .= $newline; |
| 80 | } |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 81 | |
| 82 | return $out; |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // -------------------------------------------------------------------- |
| 86 | |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 87 | |
admin | eb13db7 | 2006-09-27 00:30:48 +0000 | [diff] [blame] | 88 | /** |
| 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 | |
admin | 5abd049 | 2006-09-27 01:54:01 +0000 | [diff] [blame] | 115 | // Load the xml helper |
| 116 | $obj =& get_instance(); |
| 117 | $obj->load->helper('xml'); |
| 118 | |
admin | eb13db7 | 2006-09-27 00:30:48 +0000 | [diff] [blame] | 119 | // Generate the result |
admin | eb13db7 | 2006-09-27 00:30:48 +0000 | [diff] [blame] | 120 | $xml = "<{$root}/>".$newline; |
| 121 | foreach ($query->result_array() as $row) |
| 122 | { |
| 123 | $xml .= $tab."<{$element}/>".$newline; |
| 124 | |
| 125 | foreach ($row as $key => $val) |
| 126 | { |
admin | 5abd049 | 2006-09-27 01:54:01 +0000 | [diff] [blame] | 127 | $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline; |
admin | eb13db7 | 2006-09-27 00:30:48 +0000 | [diff] [blame] | 128 | } |
| 129 | $xml .= $tab."</{$element}>".$newline; |
| 130 | } |
| 131 | $xml .= "</$root>".$newline; |
| 132 | |
| 133 | return $xml; |
| 134 | } |
| 135 | |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | ?> |