Page 1 of 1

Help generating a csv

Posted: Wed Oct 06, 2010 7:50 am
by zero742
Hello all,

I've been working on a script to generate a csv for a client. Everything works great, except I can't get the csv to have a carriage return/line-feed. I'm sure its something obvious but I've been staring at this thing for too long and need a fresh pair of eyes. The carriage return is supposed to occur at line 10 and another at the end of the inner while loop. Thanks in advance for your help.

Code: Select all

	$filename = $_POST['filename'] . '_' . date("Y-m-d_H-i",time()) . '.csv';

	$check = mysql_query("SELECT * FROM users JOIN prefs ON (users.id = prefs.id)") or die(mysql_error());
	$total_rows = mysql_num_rows($check) or die(mysql_error());
	$k = 0;
	
	$output = 'User Id, User Level, Salutation, First Name, Last Name, Title, Company, Address Line 1, Address Line 2, City, State, Zip Code, Phone Number, Cell Number, Collection Agency, Firm, Broker, Manager, Other(specify), Affiliations, preferred_max, preferred_min_range, preferred_max_range, preferred_loan, fresh, prim, secondary, tertiary, older, other, secured, unsecured, performing, semiperforming, Non-Performing, Non-Performing, Credit Card, Consumer Loans, Medical, Installment, Payday, Storefront, Internet, Judgement, Bankruptcy, Ch 7., Ch. 11, Ch. 13, Checking, Mortgage, Residential, Commercial, First Lien, Second Lien, Automotive, Area, State;';
	$output .= '\n';
	while($k < $total_rows){
		while($row = mysql_fetch_array( $check )){
			
				$output .= $row['id'].", ";
				$output .= $row['level'].", ";
				$output .= $row['salutation'].", ";
				$output .= $row['firstname'].", ";
				$output .= $row['lastname'].", ";
				$output .= $row['title'].", ";
				$output .= $row['company'].", ";
				$output .= $row['address1'].", ";
				$output .= $row['address2'].", ";
				$output .= $row['city'].", ";
				$output .= $row['state'].", ";
				$output .= $row['zipcode'].", ";
				$output .= $row['phonenumber'].", ";
				$output .= $row['cellnumber'].", ";
				$output .= $row['faxnumber'].", ";
				$output .= $row['emailaddress'].", ";
				$output .= $row['password'].", ";
				$output .= $row['collection'].", ";
				$output .= $row['firm'].", ";
				$output .= $row['broker'].", ";
				$output .= $row['manager'].", ";
				$output .= $row['otherspecify'].", ";
				$output .= $row['affiliations'].", ";
				$output .= $row['preferred_max'].", ";
				$output .= $row['preferred_min_range'].", ";
				$output .= $row['preferred_max_range'].", ";
				$output .= $row['preferred_loan'].", ";
				$output .= $row['fresh'].", ";
				$output .= $row['prim'].", ";
				$output .= $row['secondary'].", ";
				$output .= $row['tertiary'].", ";
				$output .= $row['older'].", ";
				$output .= $row['other'].", ";
				$output .= $row['secured'].", ";
				$output .= $row['unsecured'].", ";
				$output .= $row['performing'].", ";
				$output .= $row['semiperforming'].", ";
				$output .= $row['nonperforming'].", ";
				$output .= $row['creditcard'].", ";
				$output .= $row['consumerloans'].", ";
				$output .= $row['medical'].", ";
				$output .= $row['installment'].", ";
				$output .= $row['payday'].", ";
				$output .= $row['storefront'].", ";
				$output .= $row['internet'].", ";
				$output .= $row['judgement'].", ";
				$output .= $row['bankruptcy'].", ";
				$output .= $row['ch7'].", ";
				$output .= $row['ch11'].", ";
				$output .= $row['ch13'].", ";
				$output .= $row['checking'].", ";
				$output .= $row['mortgage'].", ";
				$output .= $row['residential'].", ";
				$output .= $row['commercial'].", ";
				$output .= $row['firstlien'].", ";
				$output .= $row['secondlien'].", ";
				$output .= $row['automotive'].", ";
				$output .= $row['area'].", ";
				$output .= $row['state']."; ";

				$output .= '\n';

			}
			$k++; 	
		}
	
	$file= fopen($filename, "w") or die('Error opening file.');
	fwrite($file, $output) or die('Error writing file.');
	fclose($file)  or die('Error closing file.');
	header("Content-type: application/vnd.ms-excel");
	header("Content-Type: application/octet-stream");
	header("Content-disposition: " . $filename . ".csv");
	header( 'Content-disposition: filename="' .$filename. '"');
	print $output;

	unlink($filename);

Re: Help generating a csv

Posted: Wed Oct 06, 2010 7:56 am
by mkz
In PHP, backslash-espaced characters like \n and \t are only rendered in double quote strings.

Change the single quotes to double quotes and it should work!

Code: Select all

print '\n' // Outputs \n
print "\n" // Outputs a new line

Re: Help generating a csv

Posted: Wed Oct 06, 2010 8:02 am
by zero742
I don't know how I've never come across that issue before. Beautiful. Thanks for pointing out my blunder!

Cheers!

Re: Help generating a csv

Posted: Wed Oct 06, 2010 8:32 am
by requinix
One: fputcsv.

Code: Select all

$stdout = fopen("php://output", "w");
// for each $row {
    fputcsv($stdout, $row);
// }
fclose($stdout);
Two: lying is bad.

Code: Select all

header("Content-type: application/vnd.ms-excel");
You aren't outputting that kind of data. Don't pretend to be. Use text/csv.

Three: don't send conflicting headers.

Code: Select all

        header("Content-type: application/vnd.ms-excel");
        header("Content-Type: application/octet-stream");
        header("Content-disposition: " . $filename . ".csv");
        header( 'Content-disposition: filename="' .$filename. '"');
Use one and only one of each.