...

View Full Version : How to display paragraphs correctly?



unrelenting
02-02-2007, 11:51 PM
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:



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

GJay
02-03-2007, 05:14 PM
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 />"

unrelenting
02-03-2007, 05:19 PM
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:



<?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>';


?>

CFMaBiSmAd
02-03-2007, 05:37 PM
I'd be interested in your definition of - "I get it all clumped together" :D

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.

unrelenting
02-03-2007, 05:46 PM
I'd be interested in your definition of - "I get it all clumped together" :D

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.

:o

"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.

Len Whistler
02-04-2007, 12:45 AM
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.

unrelenting
02-04-2007, 04:39 AM
I figured it out...

nl2br($the_body)

Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum