Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP and the textarea field

    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.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    he already said "text mysql field.",
    why do u need any example? this is very basic stuff.

  • #5
    Banned
    Join Date
    Feb 2008
    Location
    Winnipeg, Canada
    Posts
    396
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Use the nl2br() function, it takes all the line breaks and changes them to <br /> tags

    Example:
    PHP Code:
    <textarea name="test"></textarea
    Becomes:

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

  • #6
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    It's not a good idea to store HTML tags in MySQL table, couse you might have problems with reusing that text later.

  • #7
    Banned
    Join Date
    Feb 2008
    Location
    Winnipeg, Canada
    Posts
    396
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Quote Originally Posted by matak View Post
    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 Code:
    <?php

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

    ?>
    Please read the thread before posting.

  • #8
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    it's never a bad idea to mention that.

    btw, nice website.

    Quote Originally Posted by Andrew Johnson View Post
    Use the nl2br() function, it takes all the line breaks and changes them to <br /> tags

    Example:
    PHP Code:
    <textarea name="test"></textarea
    Becomes:

    PHP Code:
    <?php echo nl2br($_POST["test"]); ?>
    Last edited by matak; 02-15-2008 at 06:23 PM.

  • #9
    New Coder
    Join Date
    Feb 2008
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Cheers lads,

    i now have when adding the advert

    Code:
    $description = nl2br($_POST['description']);
    and when displaying it my code is:

    Code:
    	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.

  • #10
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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.

  • #11
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    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, becaouse it's logical that it is the only word that might get snaped if it's beetween 145-150-155 characters


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •