...

View Full Version : PHP and the textarea field



cozzy1984
02-15-2008, 05:21 PM
Hi, just a quick question. I've got a description field on a form and have it to inserted into a text mysql field.

Problem is when i am displaying the data in a table with an echo statement. It doesn't output i the same way it was typed in. For instance it doesn't take new lines when i had done so we typing into the textarea. Is there a better way of doing this, possibly storing it as some other type in the database.

Also there is a problem in that if the user typed in a long continuous word without spaces, it makes the table i am displaying them in really wide and ruins the design. Is there a way round this also?

Would appreciate any ideas.

angst
02-15-2008, 05:52 PM
for your first question, there is a php function for this:

http://ca.php.net/nl2br

as for your sending question, again, there is a php function already in place for this:

http://ca.php.net/wordwrap

Jero
02-15-2008, 05:54 PM
First of all, it would be helpful to give a little more information. For instance, give us an example input and show how it is displayed in your table. And what type is that database column now?

By using the info you gave us, I'm guessing your solution lies here: http://us.php.net/nl2br

angst
02-15-2008, 05:57 PM
he already said "text mysql field.",
why do u need any example? this is very basic stuff.

Andrew Johnson
02-15-2008, 06:43 PM
Use the nl2br() function, it takes all the line breaks and changes them to <br /> tags

Example:


<textarea name="test"></textarea>


Becomes:



<?php echo nl2br($_POST["test"]); ?>

matak
02-15-2008, 07:03 PM
It's not a good idea to store HTML tags in MySQL table, couse you might have problems with reusing that text later.

Andrew Johnson
02-15-2008, 07:08 PM
It's not a good idea to store HTML tags in MySQL table, couse you might have problems with reusing that text later.

No one suggested doing that...



<?php

echo nl2br(mysql_result(mysql_query("SELECT col FROM table LIMIT 1"),0));

?>


Please read the thread before posting.

matak
02-15-2008, 07:18 PM
it's never a bad idea to mention that.

btw, nice website.


Use the nl2br() function, it takes all the line breaks and changes them to <br /> tags

Example:


<textarea name="test"></textarea>


Becomes:



<?php echo nl2br($_POST["test"]); ?>

cozzy1984
02-16-2008, 05:19 PM
Cheers lads,

i now have when adding the advert


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

and when displaying it my code is:


echo "<td colspan='2' height='60' valign='top'>".substr($description,0,150). "...</td>";

Which limits the character count to 150 i think and then puts ... at end.
I would prefer this to stop at word count rather than character count as it can stop in middle of a word, if anyone new code for this i would appreciate it.

However it still can be ruined by someone typing a contiuous long word, as it'll make the table long and upset the whole design.

angst
02-16-2008, 05:27 PM
However it still can be ruined by someone typing a contiuous long word, as it'll make the table long and upset the whole design.

did you try this function?
http://ca.php.net/wordwrap

also, you shouldn't use nl2br() on insert/update, you should only use it when displaying the data.


Which limits the character count to 150 i think and then puts ... at end.
I would prefer this to stop at word count rather than character count as it can stop in middle of a word, if anyone new code for this i would appreciate it.

there's no function for this that I'm aware of, but you could simply build a function to do this, just do a split(), and loop through the words and stop at the number of words that you desire.

matak
02-16-2008, 10:03 PM
as for wrapping your text by words you might do something with explode function of text that you shorten with substr.

something like

explode (" ", $yourtext)

will make array of words, and just slice the last word of that array (http://ca.php.net/manual/en/function.array-slice.php), becaouse it's logical that it is the only word that might get snaped if it's beetween 145-150-155 characters



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum