We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4966
    • 143 Posts
    I have a piece of code that will create a CSV file for download. The problem I’m having is the \n does not create a new line. I’ve added a \r\n and changed the document type in the header all to no avail. Here is the code:

    case "9":
    	// create csv		
    		$sql = "SELECT * 
    				FROM ".$prefix."subscribers";
    		$results = $modx->db->query($sql);
     
    		$out = '';
    		$fields = $sql =  "SHOW columns From ".$prefix."subscribers";
    		$columns = 12;
     
    		// Put the name of all fields
    		for ($i = 0; $i < $columns; $i++) {
    		$l=mysql_field_name($results, $i);
    		$out .= '"'.$l.'",';
    		}
    		$out .="\r\n";
    	 
    		// Add all values in the table
    		while ($l = mysql_fetch_array($results)) {
    		for ($i = 0; $i < $columns; $i++) {
    		$out .='"'.$l["$i"].'",';
    		}
    		$out .="\r\n";
    		}
    		// Open file export.csv.
    		$f = fopen ('export.csv','w');
    		
    		// Put all values from $out to export.csv. 
    		fputs($f, $out);
    		fclose($f);
    		
    		header('Content-type: application/csv');
    		header('Content-Disposition: attachment; filename="export.csv"');
    		readfile('export.csv');
    		break;
    


    any help would be appreciated.

    charlie
      • 26903
      • 1,336 Posts
      Try using ’PHP_EOL’ rather than \r\n, so you have $out .= PHP_EOL;
        Use MODx, or the cat gets it!
        • 4966
        • 143 Posts
        Thanks but didn’t make a difference.
          • 4041
          • 788 Posts
          Give this a try and see if it helps, all I did was comment out the \r\n spots and put the ending ’; on a new line.

          case "9":
          	// create csv		
          		$sql = "SELECT * 
          				FROM ".$prefix."subscribers";
          		$results = $modx->db->query($sql);
           
          		$out = '';
          		$fields = $sql =  "SHOW columns From ".$prefix."subscribers";
          		$columns = 12;
           
          		// Put the name of all fields
          		for ($i = 0; $i < $columns; $i++) {
          		$l=mysql_field_name($results, $i);
          		$out .= '"'.$l.'",
                          ';
          		}
          		//$out .="\r\n";
          	
          		// Add all values in the table
          		while ($l = mysql_fetch_array($results)) {
          		for ($i = 0; $i < $columns; $i++) {
          		$out .='"'.$l["$i"].'",
                          ';
          		}
          		//$out .="\r\n";
          		}
          		// Open file export.csv.
          		$f = fopen ('export.csv','w');
          		
          		// Put all values from $out to export.csv. 
          		fputs($f, $out);
          		fclose($f);
          		
          		header('Content-type: application/csv');
          		header('Content-Disposition: attachment; filename="export.csv"');
          		readfile('export.csv');
          		break;
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 3749
            • 24,544 Posts
            Try this:

            fputs($f, $out, strlen($out));



              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 4966
              • 143 Posts
              Thanks but

              Give this a try and see if it helps, all I did was comment out the \r\n spots and put the ending ’; on a new line.

              did not make a difference nor did the

              fputs($f, $out, strlen($out));

              The header code is creating a html page with the first line of the page then with the csv code. Here is a short listing. I don’t know if that make a difference. At some point I need to get rid of the header and just have the first row be the field/column names followed by the csv.

              
              Koops Mailinglist - © 2006
                 Admin Menu    Home | Send Mail | Subscribers | mailLabel1 | Add Subscriber
              "id","email","date","yourName","address1","address2","city","state","zip","country","phone","fax",/n"656","[email protected]","2010-06-26 12:13:35","","","","","","","","","", "78","[email protected]","2010-06-21 04:29:34","aaaaaaaaa","","","","","","","","", "79","[email protected]","2010-06-21 04:30:04","aaaaaaaaa","","","","","","","","", "80","","2010-06-21 04:30:41","aaaaaaaaaaa","","","","","","","","",
              

                • 6228
                • 249 Posts
                Quote from: charlie-imac at Jul 06, 2010, 02:50 PM

                The header code is creating a html page with the first line of the page then with the csv code. Here is a short listing. I don’t know if that make a difference. At some point I need to get rid of the header and just have the first row be the field/column names followed by the csv.

                If the document is viewed as HTML, line breaks will not be visible.
                  lo9on.com

                  MODx Evolution/Revolution | Remote Desktop Training | Development
                  • 4966
                  • 143 Posts
                  The document is read into a mailing list application that prints mailing labels. The new lines are not in the file and hence the problem.
                    • 3749
                    • 24,544 Posts
                    Try this (or whatever sequence is appropriate for the OS where the files are):

                    chr(13) . chr(10)
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting