[eluser]Unknown[/eluser]
Hi friends,
I have written a helper database_helper to convert query result in the csv string which can be downloaded by calling the download helper after that.
1. generate query result from $query=$this->db->query($querystmt);
2. call the helper $this->load->helper('database');
3. $outdata=create_csv_query($runquery);
4. Download the $outdata in a .csv file using the download helper.
Attached is the helper file.
Request you to kindly check and let me know if any errors are there or anyway I can enhance this.
Regards,
Anubhav Jha
I am not able to upload the helper file either in php text or html format the code is
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author Anubhav Jha
* @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
* @license http://ellislab.com/codeigniter/user-guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* CodeIgniter Directory Helpers
*
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author Anubhav Jha
* @link http://ellislab.com/codeigniter/user-guide/helpers/directory_helper.html
*/
// ------------------------------------------------------------------------
/**
* Creates a CSV file data to be downloaded from mysql query result
*
* Runs the query result gets query headers and creates a csv tring of it.
*
*
*
* @access public
* @param mysql result source
* @param bool whether to limit the result to the top level only
* @return csv string
*/
if ( ! function_exists('create_csv_query'))
{
function create_csv_query($runquery)
{
$outputdata='';
$fieldinfo='';
foreach ($runquery->list_fields() as $field)
{
$fieldinfo.=$field.",";
}
$fieldinfo=preg_replace("/,$/","",$fieldinfo);
$outputdata.="$fieldinfo\n";
foreach($runquery->result() as $info)
{
$fieldarr=explode(",",$fieldinfo);
$data='';
foreach($fieldarr as $fieldname)
{
$data.= $info->$fieldname.",";
}
$data=preg_replace("/,$/","",$data);
$outputdata.="$data\n";
}
return $outputdata;
}
}
/* End of file database_helper.php */
/* Location: ./system/helpers/database_helper.php */
?>
Do let me know how can I upload php/text files in future.