PHP Line spacing. Why is my \n not working?

Permabanned
Joined
22 Apr 2007
Posts
1,805
I'm outputting MySQL results to the screen in CSV format.

But instead of creating a new line, it sticks the results into one long line and you can see the \n on the screen.

Any help appreciated.

Thanks

Code:
<?php

include("connect.php"); 

$q = "select appliance,rating from $table";
$rs = mysql_query($q) or die("Problem with the query [$q]<br>" . mysql_error());

while ($rw = mysql_fetch_assoc($rs))
{
  // By doing this in stages its easier to read
  $temp_value = implode(',',$rw);
  echo '' .$temp_value. '\n';
}

?>
 
Thanks, I tried that though. Doesnt seem to work. Its echoing the \n onto the screen

This is what appears in the browser

res1,1.56,9\r\nres2,1.44,6\r\nres3,1.39,3\r\n etc etc
 
PHP:
echo ' ' .$temp_value. "\n";

;)

OK Marc, thanks, thats got rid of the \n from showing, but it still all appears on one line?

Its showing on an IE page, does it need some <br> or something?

EDIT: I think this may work though for what I need it for. Will advise shortly.

Thanks
 
Last edited:
the "\n" just puts a new line in your html source. of course if you want it to appear on a newline in the html itself, you'll need to use <br> or maybe use <p> tags. :)
 
But if you're outputting a CSV you'll surely want to send the mimetype as text/csv?

Code:
header('Content-Type: text/csv');

Which will have the side-effect of displaying newlines as newlines in your browser.
 
Back
Top Bottom