SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    keep form formatting when adding to database

    Hi I use a form to submit my data(a discription in this case) via php. Is there a way I can keep for formatting, namely the paragraphs? when i print them back out of my database its taken the carriage returns out.

    thanks for your time,

    Darren.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    You shouldn't change the way you store the data in the database, but apply the nl2br function when you show the text.

    The reason you should do this when displaying instead of when saving is that every time the user would save the description more <br/>'s would be added on each save (when using the "change before inserting" approach)
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply. I've tried it but it doesnt do the job.

    basicaly in the text area, when i press return for a newline can i somehow store the carriage return? i dont really want to be putting in <BR> or /n while im trying out a product description.

    Thanks

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Darren Tidmarsh View Post
    when i print them back out of my database its taken the carriage returns out.
    i bet they're still there

    html considers carriage returns as white space
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well does this mean I can do some sort of string replace before i show the product? and what code am i looking for carriage return?

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    been trying to do something like this but it doesnt work

    $row[2] = ereg_replace(" ","/n",$row[2]);

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    apparently, there is a nl2br (new line to break) function

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol i did try that but must have been using it in another way

    TY just the trick

  9. #9
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Rough outline

    PHP Code:
    // connect to db
    // assumption: some_table is empty (0 rows)
    /* assumption. $_POST['description'] = "This is a\ntest";
    */
    $desc mysql_real_escape_string($_POST['description']);
    $sql "INSERT INTO some_table(description) VALUES($desc)";
    mysql_query($sql);
    $sql "SELECT * FROM some_table";
    $row mysql_fetch_assoc(mysql_query($sql));
    echo 
    nl2br($row['description']);
    /* echoes
    This is a<br />
    test
    */ 
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •