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