Help generating a csv
Posted: Wed Oct 06, 2010 7:50 am
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.
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);