blob: 02b4b42adafb48127abd9d2c1c0836f46e16f9f0 [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
16
17// INITIALIZE THE CLASS ---------------------------------------------------
18
19$obj =& get_instance();
admin7981a9a2006-09-26 07:52:09 +000020$obj->init_class('CI_DB_export', 'dbexport');
admine79dc712006-09-26 03:52:45 +000021
admin1716d582006-09-24 20:15:03 +000022// ------------------------------------------------------------------------
23
24/**
25 * DB Exporting Class
26 *
27 * @category Database
28 * @author Rick Ellis
29 * @link http://www.codeigniter.com/user_guide/database/
30 */
31class CI_DB_export {
admin910d8622006-09-26 02:09:05 +000032
33
34 function CI_DB_export()
35 {
36 log_message('debug', "Database Export Class Initialized");
37 }
38
admin1716d582006-09-24 20:15:03 +000039 /**
adminb63ac842006-09-26 18:14:43 +000040 * Generate CVS from a query result object
admin1716d582006-09-24 20:15:03 +000041 *
42 * @access public
adminb63ac842006-09-26 18:14:43 +000043 * @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
admin1716d582006-09-24 20:15:03 +000047 */
admin910d8622006-09-26 02:09:05 +000048 function generate_cvs($query, $delim = "\t", $newline = "\n")
admin1716d582006-09-24 20:15:03 +000049 {
admin910d8622006-09-26 02:09:05 +000050 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 = '';
adminb63ac842006-09-26 18:14:43 +000056
57 // First generate the headings from the table column names
admin910d8622006-09-26 02:09:05 +000058 foreach ($query->field_names() as $name)
59 {
60 $out .= $name.$delim;
61 }
admin910d8622006-09-26 02:09:05 +000062 $out .= $newline;
63
adminb63ac842006-09-26 18:14:43 +000064 // Next blast through the result array and build out the rows
admin910d8622006-09-26 02:09:05 +000065 foreach ($query->result_array() as $row)
66 {
67 foreach ($row as $item)
68 {
69 $out .= $item.$delim;
70 }
71
72 $out .= $newline;
73 }
admin910d8622006-09-26 02:09:05 +000074
75 return $out;
admin1716d582006-09-24 20:15:03 +000076 }
77
78 // --------------------------------------------------------------------
79
admin910d8622006-09-26 02:09:05 +000080
admin1716d582006-09-24 20:15:03 +000081}
82
83?>