tqla Posted August 10, 2007 Share Posted August 10, 2007 Hi. I was hoping someone can help me. I am using this script: $file = fopen('file.csv', 'w+'); $text = ""; while ($Fields = mysql_fetch_array($result)) { $text .= $Fields['FName'] . " "; $text .= $Fields['LName'] . ", "; $text .= $Fields['CompanyName'] . ", "; $text .= $Fields['MAddress'] . " "; $text .= $Fields['MAddress2'] . ", "; $text .= $Fields['MCity'] . ", "; $text .= $Fields['MState'] . " "; $text .= $Fields['MZip'] . "\n\r"; } fwrite($file, $text); fclose($file); It works fine but the problem is that some of the fields in the database are empty so the resulting Excel file doesn't line up. What I mean is there are last names in the first name column and zip codes in the address column, etc. Is there a way to compensate for empty fields? So that all of the types will line up correctly. Thanks! Link to comment https://forums.phpfreaks.com/topic/64288-solved-help-with-csv-file-and-empty-fields/ Share on other sites More sharing options...
MadTechie Posted August 10, 2007 Share Posted August 10, 2007 a blank field should just be just blank ie testa, ,testc biggest problem is quotes maybe this $data = str_replace('"', '""', $Fields['MCity']); $text .= '"'.$data.'", '; Link to comment https://forums.phpfreaks.com/topic/64288-solved-help-with-csv-file-and-empty-fields/#findComment-320496 Share on other sites More sharing options...
tqla Posted August 10, 2007 Author Share Posted August 10, 2007 I changed it to this just to test it. (there's a comma after each filed) but it still doesn't work! $file = fopen('file.csv', 'w+'); $text = ""; while ($Fields = mysql_fetch_array($result)) { $text .= $Fields['FName'] . ", "; $text .= $Fields['LName'] . ", "; $text .= $Fields['CompanyName'] . ", "; $text .= $Fields['MAddress'] . ", "; $text .= $Fields['MAddress2'] . ", "; $text .= $Fields['MCity'] . ", "; $text .= $Fields['MState'] . ", "; $text .= $Fields['MZip'] . ",\n\r"; } fwrite($file, $text); fclose($file); MadTechie, I'm confused. How would I integrate your solution into my code? Link to comment https://forums.phpfreaks.com/topic/64288-solved-help-with-csv-file-and-empty-fields/#findComment-320539 Share on other sites More sharing options...
tqla Posted August 10, 2007 Author Share Posted August 10, 2007 bump Link to comment https://forums.phpfreaks.com/topic/64288-solved-help-with-csv-file-and-empty-fields/#findComment-320602 Share on other sites More sharing options...
Barand Posted August 10, 2007 Share Posted August 10, 2007 try <?php $file = fopen('file.csv', 'w'); while ($Fields = mysql_fetch_row($result)) { $text = '"' . join ('","', $Fields) . '"' . "\r\n"; fwrite($file, $text); } fclose($file); ?> Link to comment https://forums.phpfreaks.com/topic/64288-solved-help-with-csv-file-and-empty-fields/#findComment-320611 Share on other sites More sharing options...
roopurt18 Posted August 10, 2007 Share Posted August 10, 2007 Why don't you just do this: $file = '/path/to/file/file.csv'; $sql = "SELECT your_statement_here " . "INTO OUTFILE '{$file}' " . "FIELDS TERMINATED BY ',' " . "LINES TERMINATED BY '\n'"; if(mysql_query($sql)){ // Just a test echo file_get_contents($file); } (EDIT) Never tried this approach, but why do it in PHP when MySQL will do it faster! Link to comment https://forums.phpfreaks.com/topic/64288-solved-help-with-csv-file-and-empty-fields/#findComment-320613 Share on other sites More sharing options...
tqla Posted August 10, 2007 Author Share Posted August 10, 2007 Thanks Barand! That worked. (Now I gotta try and understand what it is you did) Roopurt18 I'm sure your mysql solution works too but I need to learn php so I must stick with it. Thanks all! Link to comment https://forums.phpfreaks.com/topic/64288-solved-help-with-csv-file-and-empty-fields/#findComment-320623 Share on other sites More sharing options...
roopurt18 Posted August 10, 2007 Share Posted August 10, 2007 Part of programming is using the proper tool for the job. There's a lot of functionality that you will needlessly write into your PHP programs that can be handled more efficiently by the database, especially things like statistics or date / time functions. So learn both! Muahahahah Link to comment https://forums.phpfreaks.com/topic/64288-solved-help-with-csv-file-and-empty-fields/#findComment-320629 Share on other sites More sharing options...
tqla Posted August 15, 2007 Author Share Posted August 15, 2007 I'll try roopurt18, thanks agian! Link to comment https://forums.phpfreaks.com/topic/64288-solved-help-with-csv-file-and-empty-fields/#findComment-324980 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.