3 replies [Last post]
mihirc
mihirc's picture
Offline
Leader
Pune, Maharashtra, India
Last seen: 5 years 43 weeks ago
Pune, Maharashtra, India
Timezone: GMT+5.5
Joined: 2007-05-09
Posts: 728
Points: 204

Hello,

I was working on a project that has been left incomplete by another developer and my client has asked me to put in a "Export to CSV function." I have used one of the functions that i have been using for quite a few days, and when i use it, it does output the necessary csv file - but throws in a lot of html output as well till i write a exit tag at the end of the code.
Here's the code: Works fine (but with lot of HTML output in the CSV file)

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
 $i = 0;
 
if (mysql_num_rows($result) > <img src="https://www.csscreator.com/sites/all/modules/smileys/packs/Roving/innocent.png" title="Innocent" alt="Innocent" class="smiley-content" /> {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].";";
$i++;}
}
$csv_output .= "\n";
 $values = mysql_query("SELECT * FROM ".$table."");
 
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}
 
$filename = $file."_".date("d-m-Y_H-i",time());
 
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
 
print $csv_output;
 
exit; 

Regards,
Mihir.

Proprietor - Thoughtfulviewfinder Services
Web Development | Design | Merchandise | Photography
My personal blog: MihirChhatre.com

Tags:
Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 10 hours 29 min ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5342
Points: 2963

Hi Mihir, You should check

Hi Mihir,
You should check what is in the table, possibly there is some empty rows or fields with unexpected values.

When you output to the screen instead of a file what are the results.

mihirc
mihirc's picture
Offline
Leader
Pune, Maharashtra, India
Last seen: 5 years 43 weeks ago
Pune, Maharashtra, India
Timezone: GMT+5.5
Joined: 2007-05-09
Posts: 728
Points: 204

Tony wrote: Hi Mihir, You

Tony wrote:

Hi Mihir,
You should check what is in the table, possibly there is some empty rows or fields with unexpected values.

When you output to the screen instead of a file what are the results.

Hey Tony,

Well i solved the problem, but it was some really bad includes (from my part). It seems that the print results doesnt like me having any html before the script. It was including all the html that was running before the screen ran and outputting it in the csv. They way i fixed was to put the whole script right at the start of the page. This did the trick. Weird. But Works. Now i can pester my client to pay me a bit Smile

Regards,
Mihir.

Proprietor - Thoughtfulviewfinder Services
Web Development | Design | Merchandise | Photography
My personal blog: MihirChhatre.com

snil19
snil19's picture
Offline
newbie
united state
Last seen: 7 years 21 weeks ago
united state
Timezone: GMT+5.5
Joined: 2012-10-09
Posts: 2
Points: 2

$list = array (

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);
 
$fp = fopen('file.csv', 'w');
 
foreach ($list as $fields) {
    fputcsv($fp, $fields);
}
 
fclose($fp);