CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   How do I insert paragraph into database, without losing line feeds? (http://www.codingforums.com/showthread.php?t=283503)

Eggweezer 12-04-2012 02:19 AM

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.

Custard7A 12-04-2012 07:03 AM

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.

Eggweezer 12-04-2012 02:12 PM

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

Fou-Lu 12-04-2012 02:29 PM

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.


Eggweezer 12-04-2012 02:47 PM

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.

Fou-Lu 12-04-2012 02:55 PM

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

Eggweezer 12-04-2012 03:15 PM

Fou-lu...You just made my day! (year maybe!)

Thank you so much. Works perfectly!

Thanks all.

Eggweezer 12-04-2012 03:22 PM

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?

Fou-Lu 12-04-2012 03:38 PM

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.

abduraooft 12-04-2012 03:40 PM

Quote:

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!)

Eggweezer 12-04-2012 04:09 PM

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>


Fou-Lu 12-04-2012 05:23 PM

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.

poyzn 12-04-2012 06:19 PM

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

Eggweezer 12-04-2012 06:25 PM

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.

Fou-Lu 12-04-2012 07:06 PM

</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)))); 



All times are GMT +1. The time now is 12:09 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.