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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Regular Coder
    Join Date
    May 2012
    Posts
    142
    Thanks
    112
    Thanked 0 Times in 0 Posts

    How do I insert paragraph into database, without losing line feeds?

    As you know, clients often like to be able to enter their own content on their websites.
    I create a simple user interface where the client can enter fields such as Name, Address, Phone, etc.
    I then insert each into the appropriate fields of my SQL database.

    My problem is when I try to insert a "whole paragraph", ( if they want to update the "News",etc on their site) that I would output into a div.

    When the user clicks "Submit", the data gets inserted into a field in my SQL database. PROBLEM: when I output it onto my page (say, into a div), it is one continuous string of data, meaning the line feeds are ignored.

    Is there a way that I can correct this issue? Or steer me in a direction? I would like to know if there is a trick to this? I cannot be the first one running into this issue.

    Any help would be appreciated! Thank you very much in advance.

  • #2
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    What do you mean by line feeds? I don't understand what is being lost, or rather what your desired result is. A paragraph seems like it should be a single string.

    Also, there is paragraph tag in HTML. Just putting it out there.

  • #3
    Regular Coder
    Join Date
    May 2012
    Posts
    142
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Let me see if I can better explain my situation.
    Here is an example of what I mean (at this link).

    http://sample.cnjwebsolutions.com/user/add.php

    I am pretty sure that the problem is that I save my $paragraph as a VARCHAR(400).
    So when I display it, it just spits it out (and ignores the line breaks).

    Maybe I have to have multiple entries for "paragraph1", paragraph2"...... and then do something like:

    if($paragraph1<>"")echo $paragraph1."</br">;
    if($paragraph2<>"")echo $paragraph2."</br">;
    if($paragraph3<>"")echo $paragraph3."</br">;

    BUT.... I was hoping there was some "canned" php code available, or a strategy that someone might have.
    I hate to have to use a massive CMS application, when many sites are small and I often code my own sites using (html/CSS).

    Any help would be greatly appreciated!.
    Last edited by Eggweezer; 12-04-2012 at 02:36 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Whitespace is ignored in HTML (as well as PHP, although a linefeed in a string is a char, so it will retain those).
    Wrap the content into nl2br() function prior to outputting (don't do it before inserting).
    That will insert a <br/> before the \n, so the HTML is given a breaking space to force the newlines.
    PHP Code:
    print nl2br("Text with \nLinefeed in it."); 
    Will result in
    Code:
    Text with <br />
    Linefeed in it.

  • Users who have thanked Fou-Lu for this post:

    Eggweezer (12-04-2012)

  • #5
    Regular Coder
    Join Date
    May 2012
    Posts
    142
    Thanks
    112
    Thanked 0 Times in 0 Posts
    I kind of understand what you are saying, but am not quite sure where to implement it. I am attaching my code, if you would be so gracious to show me.
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
      <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>

     *** <h1>Your Info</h1><br />
    <?php
            
    require ('dbstuff.php');
            
    $db connectDB();
     
            
    // get results from database 
            
    $query"SELECT * FROM mytable";   
                     
    $result=mysql_query($query)or die("Failed Query of " $query); 
    $num=mysql_numrows($result);
    mysql_close();

    $i=0;
    while (
    $i<$num) {  // beginning of outer loop

    $name=mysql_result($result,$i,"name");
    $address=mysql_result($result,$i,"address");
    $paragraph=mysql_result($result,$i,"paragraph");
    ?>


    <?php 
     
    echo "<B>Name:</B> "$name."</br>"."</br>" ;
      echo 
    "<B>Address:</B>  "$address."</br>"."</br>"."</br>";
      echo 
    "<B>Paragraph(s) below:</B>"."</br>";
     
    ?>
     <div class="paragraph">
     <?php
     
    echo " "." $paragraph";
     
    ?>
     </div>

    <?php
    $i
    ++;} // End of outer loop
    ?>
    </body>
    </html>
    Thank you.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Replace this:
    PHP Code:
     echo " "." $paragraph"
    With this:
    PHP Code:
    print nl2br($paragraph); 
    The preceding whitespace isn't necessary in your original output. To give it the "indented" look, modify the .paragraph CSS code and add a text-ident: {x}px with whatever your preferred indent choice is (1em may be a good option).

  • Users who have thanked Fou-Lu for this post:

    Eggweezer (12-04-2012)

  • #7
    Regular Coder
    Join Date
    May 2012
    Posts
    142
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Fou-lu...You just made my day! (year maybe!)

    Thank you so much. Works perfectly!

    Thanks all.

  • #8
    Regular Coder
    Join Date
    May 2012
    Posts
    142
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Last thing, and am sorry to bother you, BUT.....

    is there an easy way to put a indents (tabs... \t - I guess) at the beginning of each paragraph,
    and a break (line feed) at the end of each (to separate them?
    Last edited by Eggweezer; 12-04-2012 at 03:34 PM.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Don't. Use your CSS to add text indentation and margin's to control the space between the divs. Whitespace (such as tabs) are ignored by HTML unless they are contained in a preformatted text block.

  • #10
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    is there an easy way to put a indents (tabs... \t - I guess) at the beginning of each paragraph,
    Code:
    <p class="paragraph">
     <?php
     echo " "." $paragraph";
     ?>
     </p>
    Code:
    p.paragraph{
    text-indent:40px;
    padding:1em .2em;
    }
    (Make sure that you have no Divitis!)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Eggweezer (12-04-2012)

  • #11
    Regular Coder
    Join Date
    May 2012
    Posts
    142
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Still not quite right.

    It only indents the first paragraph, unless I missed something. http://sample.cnjwebsolutions.com/index.php
    PHP Code:
    $name=mysql_result($result,$i,"name");
    $address=mysql_result($result,$i,"address");
    $paragraph=mysql_result($result,$i,"paragraph");
    ?>


    <?php 
     
    echo "<B>Name:</B> "$name."</br>"."</br>" ;
      echo 
    "<B>Address:</B>  "$address."</br>"."</br>"."</br>";
      echo 
    "<B>Paragraph(s) below:</B>"."</br>";
     
    ?>
     <div class="paragraph">

     <p class="paragraph">
     <?php
    print nl2br($paragraph);
     
    ?>
     </p> 
     </div>

     &nbsp;
     <p><a href="user/add.php"> Back to User Interface</a></p>
     <?php

    ?>

    <?php
    $i
    ++;} // End of outer loop
    ?>

    </body>
    </html>
    Last edited by Eggweezer; 12-04-2012 at 04:12 PM.

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Define a paragraph? Your not referring to the <br/> that breaks the string are you? If so, then you'll need to instead replace the \n with the <p> tags instead of using nl2br.

    Also, you'll need to fix'em up the HTML errors as well. Rendering can be easily broken by bad HTML. Hit up the validator.w3.org to check it; I see these: </br>"."</br> as well as these: <B>Name:</B> which are not valid markup tags.

  • Users who have thanked Fou-Lu for this post:

    Eggweezer (12-04-2012)

  • #13
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    you can use wysiwyg-editors like tinymce or ckeditor. They insert paragraph tags automatically and you are also able to edit plain html in editors

  • #14
    Regular Coder
    Join Date
    May 2012
    Posts
    142
    Thanks
    112
    Thanked 0 Times in 0 Posts
    My (definition... and I probably didn't describe it right...) of paragraph, can be seen on my current page: http://sample.cnjwebsolutions.com/index.php

    See how the first paragraph indents, but the second and third do not?
    That is what I was hoping to be able to correct.

    Also, I do understand what you mean about the validating and will fix my errors. Not an expert yet, so will have to figure out why it gives me so many errors on my <b> and </br> tags.

    Thank you.

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    </br> isn't a valid tag. There is no opening <br> tag for it, so with an XHTML doctype you would use a single sided tag which is <br/>. <B> doesn't exist in XHTML standards, but you can use <b>.

    I won't follow links from work. If you mean that your string retrieved from storage should be broken into paragraphs on linefeeds, use this instead:
    PHP Code:
    printf('<p class="paragraph">%s</p>'implode('</p><p>'explode("\n"trim($paragraph)))); 

  • Users who have thanked Fou-Lu for this post:

    Eggweezer (12-04-2012)


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

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