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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post

    How to display paragraphs correctly?

    I am very new to databases but I like to dabble and have a decent understanding in all sorts of fields. I have put together a working script to post articles on my site. I am still developing it at the moment and have run into a few snags I'd like to get some help if anyone is willing to show me.

    I have an HTML submit form that submits data (a title, a link and the body) into a update_news.php file that then writes it to the database.

    I just set up one database with one table called news and in it I have 4 fields, an auto-incrementing integer, 2 varchars for the title and link and one mediumtext for the body.

    I am sending the data up like this:

    Code:
    <form action="update_news.php" method="post">
    <center>Title: <input type="text" size="50"name="title" /><br>
    Link: <input type="text" size="50" name="link" /></center><br>
    <center>Body: <br><textarea style="width: 75%" rows="10" name="body" /></textarea><br>
    <input type="submit" /></center>
    I can then display this data into a table on a separate page BUT I get it all clumped together. It doesn't display it like it was formatted in the textarea window.

    Where and how do I get it to display as it is saved to the db? Is it done on the input file or on the display file. I'm at a loss.

    Thanks for any suggestions.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    If you're using PHP, when you output the content, use the nl2br function (http://php.net/nl2br). I'd assume other languages have similar things, if only an equivalent of str_replace that can replace "\n" with "<br />"
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by GJay View Post
    If you're using PHP, when you output the content, use the nl2br function (http://php.net/nl2br). I'd assume other languages have similar things, if only an equivalent of str_replace that can replace "\n" with "<br />"
    I've seen that in my google searches but haven't figured out exactly how and where to use it.

    Here is what I have in the output file:

    Code:
    <?php
    
    
    $con = mysql_connect("localhost","root","pw");
    
    if (!$con)
    	{
    	die('Could not connect: ' . mysql_error());
    	}
    
    mysql_select_db("news", $con);
    $query="SELECT * FROM the_news ORDER BY id";
    $result=mysql_query($query);
    
    $num=mysql_numrows($result);
    
    mysql_close();
     
    echo '
    
    <table align="center" width="75%" border="0" cellspacing="4" cellpadding="8">';
    
    
    $i=0;
    while ($i < $num) {
    
    $the_title=mysql_result($result,$i,"title");
    $the_link=mysql_result($result,$i,"link");
    $the_body=mysql_result($result,$i,"body");
    
    echo '
    
    <tr>
    	<td bgcolor="#e7e6e6"><font face="Arial, Helvetica, sans-serif" color="black"></b>' . $the_title .'</b></font></td>
    </tr>
    <tr>
    	<td bgcolor="#e7e6e6"><font face="Arial, Helvetica, sans-serif" color="red"><a href="' . $the_link .'">' . $the_link . '</a></font></td>
    </tr>
    <tr>
    	<td bgcolor="#e7e6e6"><font face="Arial, Helvetica, sans-serif" color="red"><p>' . $the_body .'</p></font></td>
    </tr>';
    
    
    $i++;
    }
    
    
    echo '</table>';
    
    
    ?>

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,131
    Thanks
    2
    Thanked 328 Times in 320 Posts
    I'd be interested in your definition of - "I get it all clumped together"

    Forms use a \n as a newline character. However, in HTML, a \n simply formats the source code. Browsers need a <br /> tag for a newline. The nl2br() function searches through a string and adds the <br /> tag to every \n it finds.

    Which ever part of your data that is "all clumped together" needs to be passed through the nl2br() function before you echo it to the browser.

    Edit: The link to the PHP manual section that GJay posted, contains a working code example for the function.
    Last edited by CFMaBiSmAd; 02-03-2007 at 06:40 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by CFMaBiSmAd View Post
    I'd be interested in your definition of - "I get it all clumped together"

    Forms use a \n as a newline character. However, in HTML, a \n simply formats the source code. Browsers need a <br /> tag for a newline. The nl2br() function searches through a string and adds the <br /> tag to every \n it finds.

    Which ever part of your data that is "all clumped together" needs to be passed through the nl2br() function before you echo it to the browser.

    Edit: The link to the PHP manual section that GJay posted, contains a working code example for the function.


    "all clumped together" = There are no line breaks that would show a new paragraph has started.

    I understand what that function does but I just don't know where to place it in the code. I am learning all of this on the fly and I don't have the benefit of a teacher with a blackboard to explain where these functions should be strategically placed to work properly. I have found all sorts of explanations of these functions but not many real examples of them being used.

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by unrelenting View Post
    I have found all sorts of explanations of these functions but not many real examples of them being used.
    phpBB.com (and this forum) might be an example of what you are looking for, when you post, or edit, the output is formatted properly. If you post on the forums at phpBB.com they might be able to tell you how they handle the formating.

    Interesting how Ebay requires the user to enter the html tags, you would think they would have it setup so you wouldn't have to. I think Craiglist also requires the html tags during an edit.
    Leonard Whistler

  • #7
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    I figured it out...

    nl2br($the_body)

    Thanks.


  •  

    Posting Permissions

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