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 | |
| 34 | function CI_DB_export() |
| 35 | { |
| 36 | log_message('debug', "Database Export Class Initialized"); |
| 37 | } |
| 38 | |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 39 | /** |
admin | b63ac84 | 2006-09-26 18:14:43 +0000 | [diff] [blame^] | 40 | * Generate CVS from a query result object |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 41 | * |
| 42 | * @access public |
admin | b63ac84 | 2006-09-26 18:14:43 +0000 | [diff] [blame^] | 43 | * @param object The query result object |
| 44 | * @param string The delimiter - tab by default |
| 45 | * @param string The newline character - \n by default |
| 46 | * @return string |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 47 | */ |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 48 | function generate_cvs($query, $delim = "\t", $newline = "\n") |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 49 | { |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 50 | if ( ! is_object($query) OR ! method_exists($query, 'field_names')) |
| 51 | { |
| 52 | show_error('You must submit a valid result object'); |
| 53 | } |
| 54 | |
| 55 | $out = ''; |
admin | b63ac84 | 2006-09-26 18:14:43 +0000 | [diff] [blame^] | 56 | |
| 57 | // First generate the headings from the table column names |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 58 | foreach ($query->field_names() as $name) |
| 59 | { |
| 60 | $out .= $name.$delim; |
| 61 | } |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 62 | $out .= $newline; |
| 63 | |
admin | b63ac84 | 2006-09-26 18:14:43 +0000 | [diff] [blame^] | 64 | // Next blast through the result array and build out the rows |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 65 | foreach ($query->result_array() as $row) |
| 66 | { |
| 67 | foreach ($row as $item) |
| 68 | { |
| 69 | $out .= $item.$delim; |
| 70 | } |
| 71 | |
| 72 | $out .= $newline; |
| 73 | } |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 74 | |
| 75 | return $out; |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // -------------------------------------------------------------------- |
| 79 | |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 80 | |
admin | 1716d58 | 2006-09-24 20:15:03 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | ?> |