Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2012, 02:19 AM   PM User | #1
Eggweezer
New Coder

 
Join Date: May 2012
Posts: 98
Thanks: 76
Thanked 0 Times in 0 Posts
Eggweezer is an unknown quantity at this point
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.
Eggweezer is offline   Reply With Quote
Old 12-04-2012, 07:03 AM   PM User | #2
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
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.
Custard7A is offline   Reply With Quote
Old 12-04-2012, 02:12 PM   PM User | #3
Eggweezer
New Coder

 
Join Date: May 2012
Posts: 98
Thanks: 76
Thanked 0 Times in 0 Posts
Eggweezer is an unknown quantity at this point
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..
Eggweezer is offline   Reply With Quote
Old 12-04-2012, 02:29 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Eggweezer (12-04-2012)
Old 12-04-2012, 02:47 PM   PM User | #5
Eggweezer
New Coder

 
Join Date: May 2012
Posts: 98
Thanks: 76
Thanked 0 Times in 0 Posts
Eggweezer is an unknown quantity at this point
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.
Eggweezer is offline   Reply With Quote
Old 12-04-2012, 02:55 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Eggweezer (12-04-2012)
Old 12-04-2012, 03:15 PM   PM User | #7
Eggweezer
New Coder

 
Join Date: May 2012
Posts: 98
Thanks: 76
Thanked 0 Times in 0 Posts
Eggweezer is an unknown quantity at this point
Fou-lu...You just made my day! (year maybe!)

Thank you so much. Works perfectly!

Thanks all.
Eggweezer is offline   Reply With Quote
Old 12-04-2012, 03:22 PM   PM User | #8
Eggweezer
New Coder

 
Join Date: May 2012
Posts: 98
Thanks: 76
Thanked 0 Times in 0 Posts
Eggweezer is an unknown quantity at this point
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..
Eggweezer is offline   Reply With Quote
Old 12-04-2012, 03:38 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-04-2012, 03:40 PM   PM User | #10
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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!)
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
Eggweezer (12-04-2012)
Old 12-04-2012, 04:09 PM   PM User | #11
Eggweezer
New Coder

 
Join Date: May 2012
Posts: 98
Thanks: 76
Thanked 0 Times in 0 Posts
Eggweezer is an unknown quantity at this point
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..
Eggweezer is offline   Reply With Quote
Old 12-04-2012, 05:23 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Eggweezer (12-04-2012)
Old 12-04-2012, 06:19 PM   PM User | #13
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
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
__________________
Ushousebuilders.com
poyzn is offline   Reply With Quote
Old 12-04-2012, 06:25 PM   PM User | #14
Eggweezer
New Coder

 
Join Date: May 2012
Posts: 98
Thanks: 76
Thanked 0 Times in 0 Posts
Eggweezer is an unknown quantity at this point
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.
Eggweezer is offline   Reply With Quote
Old 12-04-2012, 07:06 PM   PM User | #15
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
</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)))); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Eggweezer (12-04-2012)
Reply

Bookmarks

Tags
input paragraph database

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:56 AM.


Advertisement
Log in to turn off these ads.