Click to See Complete Forum and Search --> : Exporting into csv extension


atortega
10-26-2004, 11:03 PM
Hi!

I have some problems when exporting my database records.

1. I want it to have a file extension of ".csv" but in my local server it always gives an ".xls" extension (ex: csv20051027.xls)"

2. When I uploaded the code to the live server, it give me a different filename with no file extension (ex: export_php).

By the way, filename of the code is export.php.

What seems to be the problem of the code?

Below is part of the code.



$csv_output = "story_id, user_id, transaction_num, lastFour, res_change, story_role, partner_fname, partner_minitial, partner_lname, partner_relationship, partner_other, partner_email, res_date, time_slot, kiosk_id, questions, pay_date, promo_code, billingAddress, billingCity, billingState, billingZip, res_type, lastmodified, lastuser";
$csv_output .= "\r\n";

$result = mysql_query("select * from myData");
$fc = mysql_num_fields( $result );

while($row = mysql_fetch_array($result))
{
for( $i=0; $i < $fc; $i++ )
{
if( $i!=0 ) $csv_output .= ",";
$csv_output .= "$row[$i]";
}
$csv_output .= "\r\n";
}

header("Content-disposition: filename=\"csv" . date("Ymd") . ".csv\"");
header("Content-type: application/vnd.ms-excel");
print $csv_output;
exit;

goeb
10-28-2004, 10:15 AM
you may try "text/csv" instead of "application/vnd.ms-excel"

atortega
10-29-2004, 01:19 AM
Thanks but it did not work either.

However, I found a way to make it work. Here's the code:



<?php

// connect to sql
include('../devscripts/db.php');

$csv_output = "story_id, user_id, transaction_num, lastFour, res_change, story_role, partner_fname, partner_minitial, partner_lname, partner_relationship, partner_other, partner_email, res_date, time_slot, kiosk_id, questions, pay_date, promo_code, billingAddress, billingCity, billingState, billingZip, res_type, lastmodified, lastuser";
$csv_output .= "\r\n";

$result = mysql_query("select * from Story_Reservations");
$fc = mysql_num_fields( $result );

while($row = mysql_fetch_array($result))
{
for( $i=0; $i < $fc; $i++ )
{
if( $i!=0 ) $csv_output .= ",";
$csv_output .= "$row[$i]";
}
$csv_output .= "\r\n";
}

$fname1 = "exportReservations.csv";
$handle = fopen($fname1, "w");
fwrite($handle, $csv_output);

header("location: " . $fname1);

exit;


?>




If you have any suggestion to improve it, I appreciate it very much.