I have an application i am writing that is trying to create a CSV file for a client.

My problem is that I cannot figured out how to make each entry on it's own line (new line). I have tried "\n" and "\n\r", but it is not working well at all.

All I get is a string with one conituous line. I need it to have one record per line.

Can anyone help me here?

Here is my code -

<?php

INCLUDE("dbcon.php");

$DBO = new DbConnect("spn");


//get all submissions

$demoSQL = "

SELECT 
abstract_demographics.first_name, 
abstract_demographics.last_name, 
abstract_status.status, 
abstract_demographics.id, 
abstract_submissions.submission_type AS sType, 
abstract_submissions.id AS subid, 
abstract_submissions.unique_identifier, 
abstract_submissions.submit_date, 
abstract_submissions.title
FROM 
abstract_submissions
LEFT JOIN 
abstract_status 
ON 
( abstract_submissions.status = abstract_status.id ), 
abstract_demographics
WHERE 
abstract_submissions.demo_id = abstract_demographics.id
ORDER BY sType

";
$demoResults = mysql_query($demoSQL);

$handle = fopen("abstract_submissions.csv","w+");


IF(is_writeable("abstract_submissions.csv"))
{

$str = "ID,STATUS,SUBID,DEMO_ID,TYPE,DATE,TITLE,LAST_NAME,FIRST_NAME";

WHILE($obj = mysql_fetch_object($demoResults))
{

$str .= $obj->unique_identifier.','.$obj->status.','.$obj->subid.','.$obj->id.','.$obj->sType.','.$obj->submit_date.','.$obj->title.','.$obj->last_name.','.$obj->first_name.'\n\r';

}//end while

//Write the line string to file

IF(fwrite($handle,$str))
{


	PRINT("<h2>FILE WRITE COMPLETE!</h2>");
}

ELSE
{
	PRINT("<h2>FILE WRITE FAILED</h2>");
}

}//end if


ELSE
{

PRINT("<h2>FILE IS NOT WRITEABLE!</h2>");

}//end else

?>

Any and ALL help would be greatly appreciated - Thanks! 🙂

    Common mistake...

    Your "\n" and/or "\r" must be enclosed in double quotes to be interpreted correctly by PHP.

      Write a Reply...