PHP-MySQL-PHP: Line skipping/spacing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

PHP-MySQL-PHP: Line skipping/spacing

Post by evilmonkey »

Hello.

If I use a form to input something (through PHP) into a MySQL database, and call on it to display, it comes out with screwed up line spacing. And line spacing is important every once in a while. I found I can eliminate this problem by putting the HTML tag <br> right into the form, but this is no good if a user is doing the input. How can this be done automatically when a user goes to the next line?

Cheers!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

which form-element do you use? <input> or <textarea>?
Maybe you want http://www.php.net/manual/en/function.nl2br.php
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Yup nl2br() the text coming out of the database. We all get stuck on that one!
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I don't really understand, can you give me an example?

I'm using textarea, and I want the <br> tag to appear whe a user goes to the next line.

Bieng more clear, the way the text is entered and retrieved now, where the line ends depends on the size of the ie/netscape winodw. I want it to end whereever the user wanted it it to end.

Thanks.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

wordwrap()?

Never used that much since it doesn't work on my local windows server for some reason but it might help.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

No, that does not help. This is what i already have. The nl2br was on the right track, i just don't understand how to use it.

Thanks for trying though.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

When you retreive text from the database, you'll have something like:

$result = mysql_fetch_array($query);
$text = $result['text'];

replace with

$text = nl2br($result['text']);

If you addslashed on the way in, don't forget to striplash as well.
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

If that doesn't work, add wrap="physical" to the textarea tag
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You don't really want to have <br> tags appearing in the text area because although you can't see it newlines characters are being added each time the person presses enter. What nl2br() does is, when you want to redisplay this information, it adds an HTML linebreak (<br />) next to the newline character so not only does the source have the newlines apparent, the output on the screen does too.

Mac
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

McGruff, thank you very much m8, that worked. Thanks to everyone else who contributed to the topic as well.
Post Reply