Page 1 of 1

Line breaks in CSV export

Posted: Fri Jun 05, 2009 4:12 am
by sirstrumalot
I have a form using fopen to make a csv and write to the file. I then later import the csv data into a MySQL database. It needs to be one row and since I set this up for fopen, I get a form export that randomly may have 2,3, or 4 lines instead of one. I'm guessing I need to trim and addslashes, but I attempted and failed. Here's my code: (oh, and the code below has added line returns in the $_POST section to make it easier on the screen, in the file, it's contiguous with commas separating values)

Here's my code:

Code: Select all

 
//CSV DATA FROM FORM
            $csv_output = '"'.$_POST['daily_aptDate'].'","'.$_POST['daily_timeIn'].'","'.$_POST['daily_timeInAMPM'].'",
           "'.$_POST['daily_timeOut'].'","'.$_POST['daily_timeOutAMPM'].'","'.$_POST['daily_therapyType'].'",
           "'.$_POST['daily_addNotes'].'"';
            
            //CREATING CSV FILE AND APPENDING SINGLE ROW OF DATA
            $handle = fopen($filename, 'w+');
            fwrite($handle, $csv_output);
            fclose($handle); 
            
            //REDIRECT TO SIGNATURE CAPTURE 
            $redirect = "sigcapture/daily/patient/capture.php";
            header('Location: '.$redirect); die;
 
Please help, I am hoping to stick to my deadline. This is happening with three forms.

Re: Line breaks in CSV export

Posted: Fri Jun 05, 2009 6:19 am
by Mark Baker
Why not use PHPs built-in csv writing functions?

Code: Select all

 
$csv_output = array($_POST['daily_aptDate'], $_POST['daily_timeIn'], $_POST['daily_timeInAMPM'],
                    $_POST['daily_timeOut'], $_POST['daily_timeOutAMPM'], $_POST['daily_therapyType'],
                    $_POST['daily_addNotes']
                   );
            
//CREATING CSV FILE AND APPENDING SINGLE ROW OF DATA
$handle = fopen($filename, 'w+');
fputcsv($handle, $csv_output);
fclose($handle); 
 
Note that if a field in the data (such as $_POST['daily_addNotes']) contains line breaks, then these will appear in the CSV file, so the single record will be spread over several lines. Thsi is perfectly valid for a CSV file.

Re: Line breaks in CSV export

Posted: Fri Jun 05, 2009 7:58 am
by sirstrumalot
Okay, well then below is my method of importing into the database. Would this method be suitable for importing into the database if in fact there are line breaks due to textareas and other places the user might insert line breaks?

Code: Select all

 
if(isset($_POST['Submit'])){
$table = trim($_POST['tableType']);
 
$handle = fopen ($_FILES['csv']['tmp_name'],"r");
                    
while ($data = fgetcsv ($handle, ",")) {
                        
 
if ($table == "missedVisit") {
  $insertSQL = sprintf("INSERT INTO missedVisit (missed_fname, missed_middle, missed_lname, //etc.... ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, //etc....)",
                    GetSQLValueString($data['0'], "text"),
                                        GetSQLValueString($data['1'], "text"),
                                        GetSQLValueString($data['2'], "text"),
                                        GetSQLValueString($data['3'], "text"),
                                        GetSQLValueString($data['4'], "text"),
                                        //etc...
 
  mysql_select_db($database_contacts);
  $Result1 = mysql_query($insertSQL) or die(mysql_error());
    $cid = mysql_insert_id();
 
mysql_query("INSERT INTO historyMissed (missedHistory_contact, missedHistory_date, missedHistory_status) VALUES
(
    ".$cid.",
    ".time().",
    1
)
");
}
 

Re: Line breaks in CSV export

Posted: Fri Jun 05, 2009 8:18 am
by Mark Baker
Why not try it and see.

An alternative, if it doesn't work, would be "escaping" the new line characters before the fputcsv, and unescaping them after the fgetcsv

Re: Line breaks in CSV export

Posted: Fri Jun 05, 2009 3:40 pm
by sirstrumalot
Looks like we're on the right track. Thanks!