views:

75

answers:

4

Hey !

I started doing a new project in PHP / MySql . The Aim of this project is to manage articles for a magazine that i am the editor of.

So the content of the articles, i decided i would store with the "TEXT" column type of MySql

Now when i retrieve this column and print it with echo, the newlines are not there. Its all on the same line.

$resset = mysql_query("select txt from articles where id = 1");
$row = mysql_fetch_assoc($resset);
$txt = $row['txt'];
echo $txt; //doesnt print it as it is in the database as it was typed (multiline)

Find below, the text as it looks in the database and as it looks when it is echoed

in the databse, it is with new lines

Text as it looks when echod

But within the database, its stored with newlines.

Has anybody else encountered this problem?

Please help me as my project depends on this :|

+5  A: 

Whitespace in HTML is folded into a single space. Use nl2br() if you want to maintain newlines in HTML.

Ignacio Vazquez-Abrams
sweet. Thanks for the awesome quick answer. You SAVED ME !!! |^^|
Skun
A: 

Have you tried

echo $txt."<br/>";
Sean
cant do this cause the breaks need to be inside the $txt ! nl2br does the work beautifully !
Skun
A: 

alternatively, you can put your output between <pre> tags:

echo "<pre>";
$resset = mysql_query("select txt from articles where id = 1");
$row = mysql_fetch_assoc($resset);
$txt = $row['txt'];
echo $txt; //doesnt print it as it is in the database as it was typed (multiline)
echo "</pre>";

btw,

echo $txt."<br/>";

may not work since it will just append newline at the end but not within $txt string

daghan
A: 

I know this isn't part of your question, but if you additionally want new lines in your HTML code but not your presentation, use double quotes with \n. This can help keep the HTML really tidy.

echo "\n";
Lotus Notes
that i know :) thanks !
Skun