Line breaks in CSV export

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sirstrumalot
Forum Commoner
Posts: 27
Joined: Mon May 18, 2009 10:26 pm

Line breaks in CSV export

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Line breaks in CSV export

Post 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.
sirstrumalot
Forum Commoner
Posts: 27
Joined: Mon May 18, 2009 10:26 pm

Re: Line breaks in CSV export

Post 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
)
");
}
 
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Line breaks in CSV export

Post 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
sirstrumalot
Forum Commoner
Posts: 27
Joined: Mon May 18, 2009 10:26 pm

Re: Line breaks in CSV export

Post by sirstrumalot »

Looks like we're on the right track. Thanks!
Post Reply