SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Location
    Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP / MySQL based forum

    Okay now, first question.

    I am getting the body of the forum post from a textarea box and inserting it into a mysql database.

    The problem is, that the mysql database is not storing the line breaks between sentances or paragraphs.

    If anyone knows how to get around this it would be of greate help.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  2. #2
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, MySQL should be storing the line breaks, however, since HTML has no formatting, when the data is pulled out of the database, the line breaks do not appear.

    To get around this it would be wise to use a string replacement function to transform all of the line breaks and carriage returns into valid HTML line breaks and carriage returns.

    An example would be something like this:
    PHP Code:
    <?php
    // pull data from database
    // text area data is named $content
    $fromdb=array('\n','\n\n','\n\r','\r'):
    $tohtml=array('<br />','</p><p>','</p><p>','</p><p>');
    $content=str_replace($fromdb,$tohtml,$content);
    echo(
    $content);
    ?>
    Does that help?

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  3. #3
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when inserting into table use

    PHP Code:
    $text nl2br($text); 
    http://redgoals.com ... my site ... nuff said

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Location
    Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that.

    The second question that I have is when using the echo command to display html tags, it doesn't generate a new line (when viewing the html code) for each new echo command.

    Is there a way around this, such as using the \n in some way or by using a different command that does a similar thing.

    Thanks,
    WebmasterMattD
    WebmasterMattD.NET

  5. #5
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    \n will print a new line in the outputted html.

    or you could use a combination of

    \n

    or

    \r

    or

    \r\n

    depending on your os.

    gotta dash, got college.
    http://redgoals.com ... my site ... nuff said

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by redgoals
    when inserting into table use

    PHP Code:
    $text nl2br($text); 
    Use nl2br() but use it before displaying the data. Always try to store a "clean" copy of the data in the database

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by redgoals
    when inserting into table use
    PHP Code:
    $text nl2br($text); 
    actually you should store the data "as-is" with the newlines and all... only when you want to display it do you apply nl2br()...

    you'll never know when you want those newline characters again...

    <<edit>> oops... i jumped onto that immediately so i failed to see seanf's post...
    Last edited by redemption; Mar 18, 2002 at 07:17.

  8. #8
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    People may take double the notice of it!

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Location
    Australia
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I guese since nl2br() was meantioned in three different posts that was why I implemented it into the site.

    Later,
    WebmasterMattD
    WebmasterMattD.NET


Bookmarks

Posting Permissions

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