Welcome Guest, Not a member yet? Register   Sign In
db to csv - solved
#1

[eluser]Near[/eluser]
hi its me again.. someone please help me generate a downloadable csv file from a query
i tried

Code:
$this->load->dbutil();
$query = $this->db->query("SELECT * FROM mytable");
echo $this->dbutil->csv_from_result($query);

that i found at the user manual but it only echoes the query result at the browser and did not generate csv files. thanks
#2

[eluser]Mirge[/eluser]
[quote author="Mbet" date="1313147187"]hi its me again.. someone please help me generate a downloadable csv file from a query
i tried

Code:
$this->load->dbutil();
$query = $this->db->query("SELECT * FROM mytable");
echo $this->dbutil->csv_from_result($query);

that i found at the user manual but it only echoes the query result at the browser and did not generate csv files. thanks[/quote]

Untested.

Code:
$outputFile = 'myfile.csv';

$fp = fopen($outputFile, 'w') or die("Unable to open $outputFile for writing.");
fwrite($fp, $this->dbutil->csv_from_result($query);
fclose($fp);
#3

[eluser]Near[/eluser]
hi i use the file helper. that takes care of the writing in the file.

my new code looks likes this

Code:
function getcsv() {
    $this->load->helper('file');
    $this->load->dbutil();
    
    $delimiter = ",";
    $newline = "\r\n";
    $query = $this->db->query("SELECT * FROM stations");    
    $csv = $this->dbutil->csv_from_result($query,$delimiter,$newline);
    if ( !write_file('./uploads/', $csv))
    
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}

is there any way to tell the browser that i was generating a file and ask the user if he wants to download it?
#4

[eluser]Mirge[/eluser]
http://stackoverflow.com/questions/14655...-using-php
#5

[eluser]Near[/eluser]
thanks the browser do a force download on that file but the file that is downloaded is empty
#6

[eluser]Near[/eluser]
[quote author="Mbet" date="1313150235"]thanks the browser do a force download on that file but the file that is downloaded is empty[/quote]

never mind this post..

here my code.
Code:
function getcsv() {
    $this->load->helper('file');
    $this->load->dbutil();
    $delimiter = ",";
    $newline = "\r\n";
    $query = $this->db->query("SELECT * FROM benchmarks");    
    $csv = $this->dbutil->csv_from_result($query,$delimiter,$newline);
    if ( !write_file('./uploads/csv1.csv', $csv))
    
{
echo 'Unable to write the file';
}
else
{
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="csv1.csv"');
header('Pragma: no-cache');
readfile("./uploads/csv1.csv");
}
    }
#7

[eluser]Near[/eluser]
THANKS Mirge :-)
#8

[eluser]Mirge[/eluser]
welcome




Theme © iAndrew 2016 - Forum software by © MyBB