Re: [PHP] textarea new line to mysql database
Well, instead of storing the text from the textarea directly into the db, validate it and wrap it with tags (replace \n) and then store it. This way you needn't use nl2br every time you retrieve the text from db. -Sterex On Fri, May 15, 2009 at 7:42 PM, Stuart wrote: > 2009/5/15 PHPScriptor : > > > > Hello, > > > > How do you guys handle this "problem". > > > > Just a form with a textarea. When I use enters in the textarea it's saved > to > > the db like this: > > > > database: > > "first line > > second line" > > > > when I edit the value in the form: > > "first line > > second line" > > > > when I output the value to html: > > "first linesecond line" (unless I use nl2br()) > > > > Is there a way that I could save it to the db that will work for db, > output > > and edit without using any other function like nl2br? > > What's your problem with using nl2br? This is the reason it exists!! > > Store the raw data in the database, and run nl2br on it when you > display it. I don't see a "problem". > > -Stuart > > -- > http://stut.net/ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] textarea new line to mysql database
Well, its not exactly a 'problem' at all. The textarea just does what its meant to do - accept raw text input and lets you process it via form action. This is neither a PHP or a HTML 'problem'. The reason you cannot see the line breaks when you echo the text on your browser is the fact that the browser does not recognize '\n' as a line break; it only recognizes ''. Coming to the tag, stands for preformatted; it tells the browser to output the text in the raw format as it is. Hence it displays properly. -Sterex On Fri, May 15, 2009 at 7:54 PM, PHPScriptor wrote: > > Yes, I thought about that. But then you have a problem when you're going to > 'edit' that data back in a form. Then you get "first linesecond line" > in your textarea. > > > Manoj Sterex wrote: > > > > Well, instead of storing the text from the textarea directly into the db, > > validate it and wrap it with tags (replace \n) and then store it. > > This way you needn't use nl2br every time you retrieve the text from db. > > > > -Sterex > > > > > > On Fri, May 15, 2009 at 7:42 PM, Stuart wrote: > > > >> 2009/5/15 PHPScriptor : > >> > > >> > Hello, > >> > > >> > How do you guys handle this "problem". > >> > > >> > Just a form with a textarea. When I use enters in the textarea it's > >> saved > >> to > >> > the db like this: > >> > > >> > database: > >> > "first line > >> > second line" > >> > > >> > when I edit the value in the form: > >> > "first line > >> > second line" > >> > > >> > when I output the value to html: > >> > "first linesecond line" (unless I use nl2br()) > >> > > >> > Is there a way that I could save it to the db that will work for db, > >> output > >> > and edit without using any other function like nl2br? > >> > >> What's your problem with using nl2br? This is the reason it exists!! > >> > >> Store the raw data in the database, and run nl2br on it when you > >> display it. I don't see a "problem". > >> > >> -Stuart > >> > >> -- > >> http://stut.net/ > >> > >> -- > >> PHP General Mailing List (http://www.php.net/) > >> To unsubscribe, visit: http://www.php.net/unsub.php > >> > >> > > > > > > > - > visit my website at http://www.phpscriptor.com/ > http://www.phpscriptor.com/ > -- > View this message in context: > http://www.nabble.com/textarea-new-line-to-mysql-database-tp23560478p23560882.html > Sent from the PHP - General mailing list archive at Nabble.com. > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] textarea new line to mysql database
@Robert: True. I was assuming that the text was going to be final and not being edited again. @PHPScriptor: If you do have a lot of textareas to work around with, why don't you give TinyMCE a try. Thats the best option you have got. It replaces the textarea into more of a html compatible one and when you are editing the text again, it will be properly formatted. You'll also get toolbars for text editing etc., more like your mail compose window right now. :) -Sterex On Fri, May 15, 2009 at 7:56 PM, Robert Cummings wrote: > On Fri, 2009-05-15 at 19:48 +0530, Manoj Sterex wrote: > > Well, instead of storing the text from the textarea directly into the db, > > validate it and wrap it with tags (replace \n) and then store it. > > This way you needn't use nl2br every time you retrieve the text from db. > > Don't do that unless it's a cached entry in the DB. Unless you > absolutely know you'll never need the raw text again, you should always > store the raw text so it can be processed in the future in any way you > see fit. If you want to speed up the process of conversion, use an > additional field in the database, or a cache, that contains the > processed content. > > Cheers, > Rob. > -- > http://www.interjinn.com > Application and Templating Framework for PHP > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] textarea new line to mysql database
TinyMCE: http://tinymce.moxiecode.com/ -Sterex On Fri, May 15, 2009 at 7:56 PM, Paul M Foster wrote: > On Fri, May 15, 2009 at 07:03:49AM -0700, PHPScriptor wrote: > > > > > Hello, > > > > How do you guys handle this "problem". > > > > Just a form with a textarea. When I use enters in the textarea it's saved > to > > the db like this: > > > > database: > > "first line > > second line" > > > > when I edit the value in the form: > > "first line > > second line" > > > > when I output the value to html: > > "first linesecond line" (unless I use nl2br()) > > > > Is there a way that I could save it to the db that will work for db, > output > > and edit without using any other function like nl2br? > > If I understand your question, the answer is no. If you have wrap="hard" > as an attribute for your textarea, it will store the data with CR/LF. > But CR/LF don't show up as a line ending when displayed in HTML. You > have to use nl2br() to translate for HTML. It also may be that you need > to do a translation from CR/LF (textarea line ending) to LF (*nix line > ending). The CR/LF *will* show up properly when *editing* in the > textarea field, just not when displayed without a textarea. > > Paul > > -- > Paul M. Foster > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] textarea new line to mysql database
@tedd: Its just another way of looking at the things. Putting HTML into the DB is not really wrong (perhaps in this context it is). If you do have HTML in the DB, you can directly echo it out and use CSS to style it accordingly. Just my 2 cents. :) -Sterex On Fri, May 15, 2009 at 8:24 PM, tedd wrote: > At 7:22 AM -0700 5/15/09, PHPScriptor wrote: > >> Well, the problem is that I have a lot of forms, a lot of data to output, >> and >> even then, I don't know always where I have a textarea or just a >> inputfield. >> But true, I could even set the nl2br on an input field, it wouldn't make a >> difference. >> But I just don't understand why this problem exists? What's the reason? >> > > > Simply, the problem deals with how different systems handle the "end of > line" (EOL) character? > > You can read more about it here: > > http://en.wikipedia.org/wiki/Newline > > What you've encountered (IMO) is just another extension/example of the > problem. > > Now, your choices are to: > > 1. Listen to Rob (the wisest) and use the tag. > > 2. Listen to Stuart (the wisest) and use the nl2br() function > > 3. Listen to Sterex (IMO -- who is totally wrong) and put html in your > database; > > 4. Or, listen to me (who is somewhere between Rob/Stuart and Sterex) and > use either the tag or the nlbr() function depending upon what you want > to do with the output. Both solutions [1 and 2] provide different ways to > handle data. Number [3] simply creates another problem you, or someone else, > will have to deal with later on. > > Cheers, > > tedd > > -- > --- > http://sperling.com http://ancientstones.com http://earthstones.com > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >