Solved

How can I place a carriage return in text field from database?

Posted on 2009-05-02
7
334 Views
Last Modified: 2014-11-12
I have setup a multi-line text field for entry to a database. Many users try separating items and tesk by hitting enter supplying them with a new line within the text field although when the data is queried and displayed in the page it does not show the carriage return that has been used for oganization.

I have seen this on Google calendar and I am trying to get that look. How is this done?

Thank you all,
gorg
0
Comment
Question by:fun_g
  • 4
  • 3
7 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24288487
Use the nl2br() function:

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

This function converts linefeeds to <br> tags, so that they break lines in html.
0
 

Author Comment

by:fun_g
ID: 24288521
cxr,
Thank you very much for your quick response.
This function looks like it is more geared for line by line query from the database. I am submitting a text field and calling the text field to display the data on the page. Within the text field I need to preserve the carriage returns for display within the text box on the page.

Thank you,
gorg
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24289426
Can you show your code?

>> for display within the text box

What kind of text box? A <textarea>? Or a paragraph <p> or <div>?

For a textarea, no conversion should be necessary. For a paragraph or div, use nl2br().
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:fun_g
ID: 24289967
The entry in the form is a <textarea> but the code that I am calling it back with is listed below. How can I call it to a <textarea>. Lack of knowledge here. :(



$return .= "<td>";
														$tmp = ($dietsheet->text_5) ? $dietsheet->text_5 : "&nbsp";
$return .= "<div id=\"tDSText5_$dietsheet->cf_id\">{$tmp}</div>";
$return .= "<input id=\"tbDSText5_$dietsheet->cf_id\" style=\"display: none; float: left; width: 70%\" type=\"text\" value=\"{$dietsheet->text_5}\">";
$return .= "</td>";

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24290669
Have you tried nl2br()? Try the code below.

>> How can I call it to a <textarea>

"call it"... do you mean display it in a textarea? Just wrap it in a textarea tags:

$return .= "<div id=\"tDSText5_$dietsheet->cf_id\"><textarea>{$tmp}</textarea></div>";

If you use a textarea, you should NOT use nl2br().

PS: I added a semicolon to your &nbsp; In many cases the browser will understand it without the ; but it is correct to end it with a ; character.
$tmp = ($dietsheet->text_5) ? nl2br($dietsheet->text_5) : "&nbsp;";

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24290676
Heh! I forgot, EE removes the &nbsp; from the code snippet. The " " at the end of the code snippet contained &nbsp; (nonbreakable space)
0
 

Author Comment

by:fun_g
ID: 24292210
cxr,
The addition 'n12br' worked perfect. Thank you very much. This has loaded the page with the correct format. Thank you for your time.
gorg
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This Micro Tutorial will demonstrate how to analyze your website's back links using a network graph powered by a fusion table.
This Micro Tutorial will demonstrate Google Calendar to monitor updates with top sites, such as Facebook, Google, Twitter, etc. with Marketing News. Each update of Google Calendar can be monitored, correlate dips and spikes in your website traffic, …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question