Forum Moderators: coopster

Message Too Old, No Replies

Want to leave line break but does not work

Newbie asks help

         

blue_eagle

5:36 pm on May 17, 2006 (gmt 0)

10+ Year Member



Hi,

First of all let me tell you that i am very new to php. I am getting some hands on practice with trial and error while i am customizing some sections of some scripts.

These days, I am working on entering data through php file to mysql. Everything going fine so far. The only problem i am having is. Some fields i have to leave line breaks. I created scrolling text box on my template and enter there with line breaks for example:

Business Hours:

Monday - Friday
9.00 AM - 4.00 PM

Sunday
Closed

i enter excatly like this and since the field i specified in mysql is text it seems ok there too. However, when i want to display it on my html template it comes like this.

Monday - Friday9.00 AM - 4.00 PMSunday Closed

I cant get rid off from this problem and i would definetly appreciate if someone can let me know what is the solution.

Thanks in advance.

jatar_k

5:42 pm on May 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when you submit the data you could use nl2br [php.net] to convert your line breaks to <br> then you should have no problems with display in html

blue_eagle

5:50 pm on May 17, 2006 (gmt 0)

10+ Year Member



Thanks jatar,

Should i used this function when i am submitting the data i mean should i use it on the form page that i submit the data or on the template page where i display the data.

Also, i would definetly appreciate if someone can write me a sample to use it. I was checking php.net web site about this function but it seems kinda confusing i tried some but didnt work

Thanks a lot

jatar_k

5:54 pm on May 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



your other option is to just type in the <br> when you enter the data, even easier

I would use it when you submit it, before it is entered into the database

blue_eagle

5:55 pm on May 17, 2006 (gmt 0)

10+ Year Member



Thanks jatar, but since people who register will enter their data using <br> after each line doesn;t seem very useful. Thanks anyway.

jatar_k

6:06 pm on May 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



alright, thought it was a personal use thing, after the data is submitted then

is this information editable?
if it is then when you load the form you need to strip out the <br /> from the string or it will get an extra set of breaks everytime they edit

example for insertion (only for \n transform other security measures omitted)

if you have a textarea called "bizhrs" and they enter data as you showed above and then hit submit you would have this data available in the $_POST array. You could then use nl2br before you pass it to your insert

$bizhrs = nl2br($_POST['bizhrs']);

you could then use it in your insert

if it is editable then after you do your select to get the data then you could use str_replace [php.net] before you populate the textarea again. Using the same varname to represent the column from the db

$bizhrs = str_replace('<br />,'',$row['bizhrs']);

make sense?

blue_eagle

10:41 pm on May 17, 2006 (gmt 0)

10+ Year Member



Thank you very much jatar