Welcome Guest, Not a member yet? Register   Sign In
Displaying text from textarea.
#1

[eluser]donald.tbd[/eluser]
Hello,

I have a problem displaying text i wrote into a textarea and saved into a database.
For example i wrote:
Hello
my
name
is
Donald

Now if i just select it from database and echo it into a blank page i get "Hello my name is Donald" all in one row. If i echo it into a textarea i get the result i wrote, every word on seperate row.
How can i get the same result on a blank page?

Cheers!
#2

[eluser]TWP Marketing[/eluser]
I found this in an old post from 2004 and modified it for your case. It 'should' still work with today's browsers... The html textarea will return some form of the newline tag to do linebreaks. It is browser specific, but there are only a limited number of choices.

If this fails, check on the php function nl2br().
Code:
<?php

//Something of a "hack" to get around the different newline characters different
//OSes use.

$textarea = str_replace("\r\n", "\n", $_POST['textarea']);
$textarea = str_replace("\r", "\n", $textarea);

//Split the textarea by new line

$textarea = explode("\n", $textarea);

//Now we have each line as an element in the array
//Loop through each line, echoing

foreach ($textarea as $line) {

    echo($line);

}
?>
#3

[eluser]John_Betong_002[/eluser]
@TWP Marketing

edit
Whoops - I never noticed your reference to nl2br()


http://php.net/manual/en/function.nl2br.php


 
 
 
#4

[eluser]donald.tbd[/eluser]
Thanks, nl2br did the trick. Now the question is can i put it somewhere in the code so i dont have to write it in the view part every time i need to display a textarea content. Or is it better to write it in the view?
#5

[eluser]John_Betong_002[/eluser]
[quote author="donald.tbd" date="1308284899"]Thanks, nl2br did the trick. Now the question is can i put it somewhere in the code so i dont have to write it in the view part every time i need to display a textarea content. Or is it better to write it in the view?[/quote]

Try it and see if there is a difference between view, controller or model.

Let us know if you have any problems.

Usually I use nl2br() in the model.

It is up to you where you use it.
 
 
 




Theme © iAndrew 2016 - Forum software by © MyBB