benjancewicz Posted July 9, 2007 Share Posted July 9, 2007 Alright. I've got this form: http://barakdrama.org/contact%20beta.htmlIt writes to a .CSV form, using this code: function write_to_csv( $records ) {$fields = $_POST;unset($fields['submit']);$csv_row = implode(',', str_replace(',', ' ', $fields )); }$my_csv_data = write_to_csv( $_REQUEST['person'] );$file = "output.csv";$fh = fopen($file, 'w') or die("can't open file");fwrite($fh, $csv_row);fclose($fh); However, I noticed that when I typed any commas in the "Comments" field (or any other, for that matter) it really screwed up my CSV file.How do keep it from doing that? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 9, 2007 Share Posted July 9, 2007 A CSV file is a file where the fields are separated by commas, so you have any commas in the fields it will interfere with the format. You might be able to escape the commas in the text with a backslash, but the algorithm for parsing the CSV would need to be a little more complex. It sort of depends how the program that creates the CSV file stores a comma in the text. Link to comment Share on other sites More sharing options...
vijay Posted July 9, 2007 Share Posted July 9, 2007 Hi.. For those type of fields like "Comment,address".. make your own function which will convert/escape it into one unique entity and then go for next operation..Regards,Vijay Link to comment Share on other sites More sharing options...
benjancewicz Posted July 9, 2007 Author Share Posted July 9, 2007 Hi.. For those type of fields like "Comment,address".. make your own function which will convert/escape it into one unique entity and then go for next operation..Regards,VijayHow do I do that? (I'm very new to this) Link to comment Share on other sites More sharing options...
jhecht Posted July 10, 2007 Share Posted July 10, 2007 My Solution to this may be a bit simple minded, but honestly i think when you get the contents for the form, you just run all of the variables through something like this: <?php <span class="postcolor">function write_to_csv( $records ) { $fields = $_POST; unset($fields['submit']); //Unset that; $new_array = array(); foreach($fields as $field=>$field_value){ $new_array[$field]= str_replace(",",",",$field_value); } $csv_row = implode(',', $new_array); return $csv_row; } $my_csv_data = write_to_csv( $_REQUEST['person'] ); $file = "output.csv"; $fh = fopen($file, 'w') or die("can't open file"); fwrite($fh, $csv_row."\n"); fclose($fh); </span> ?> Then whenever you read the file you could simply explode the file by the \n character, and if you must read the contents have it replace "," with the "," character. Link to comment Share on other sites More sharing options...
benjancewicz Posted July 19, 2007 Author Share Posted July 19, 2007 I understand what you're doing, but it doesn't seem to be writing to the file. The file is ending up blank.Any ideas?Oh, and I took off the <span> class... it seemed to make it not process. Link to comment Share on other sites More sharing options...
jhecht Posted July 19, 2007 Share Posted July 19, 2007 <?phpfunction write_to_csv( $records ) {$fields = $_POST;unset($fields['submit']);//Unset that;$new_array = array();foreach($fields as $field=>$field_value){ $new_array[$field]= str_replace(",",",",$field_value);}$csv_row = implode(',', $new_array); return $csv_row;}$my_csv_data = write_to_csv( $_REQUEST['person'] );$file = "output.csv";$content = file_get_contents($file);$content .= "\n".$my_csv_data;file_put_contents($file,$content);?> Generally i just use file_get_contents()/file_put_contents() for handling things with files. You should look up the documentation on them. May save you a bit of time. Link to comment Share on other sites More sharing options...
benjancewicz Posted July 23, 2007 Author Share Posted July 23, 2007 Ok, I tried using this code and it's still not working. In fact I keep getting an error reading: "Fatal error: Call to undefined function: file_put_contents() in /home/barakdra/public_html/contact.php on line 162"I did read up on these functions, but I'm still at a loss. Essentially what does the last part do?$file = "output.csv";$content = file_get_contents($file);$content = "\n".$my_csv_data;file_put_contents($file,file_append); What is the purpose? I feel like if I understood that I could better understand how to implement it. Here is the link to the form: http://barakdrama.org/contact%20beta.htmlAnd here is the processing part: http://barakdrama.org/contact.phpI appreciate all your help. Thanks. Link to comment Share on other sites More sharing options...
jhecht Posted July 23, 2007 Share Posted July 23, 2007 You must be on a PHP 4 server. The altered code for PHP 4 is something like this: $file = 'output.csv';$fResource = fopen($file,'r');$contents = fread($fResource,filesize($fResource))."\n".$my_csv_data;fclose($fResource);$fResource = fopen($file,'w');fwrite($fResource,$contents); Although i believe instead of having to open the file twice you could just use the r+ method(instead of just 'r') and you wouldn't need to get the contents to add them to the end, you could just use it to write the line at the beginning of the file. Link to comment Share on other sites More sharing options...
Synook Posted July 23, 2007 Share Posted July 23, 2007 I was just thinking, you could use a tab delimited file instead of a comma delimited one, as there are very unlikely to be tabs in any field, and you can just replace them out. However, I suppose what you have to do with the resulting file will influence your decision. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.