I need a line break when extracting some text from a MySQL d

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
morena84
Forum Newbie
Posts: 9
Joined: Thu Apr 03, 2008 9:52 am

I need a line break when extracting some text from a MySQL d

Post by morena84 »

Hi there.
In trouble again. I had a look around but could not find the right solution.

I have a blog that saved the blog entries into a MySQL database.
A form transfers the data into the databse:

Form:

Code: Select all

 <form action="../ok.php" method="post" name="insert" class="form" id="insert"><p><label for="title">Title<input name="title" type="text" class="form" id="title" size="60" maxlength="250" ></label></p><p><label for="text">Text<textarea name="text" cols="60" rows="16" class="form" id="text"></textarea></label></p><p><label for="author">Author<input name="author" type="text" class="form" id="author" size="60" maxlength="100" ></label></p><label><input type="submit" name="send" id="send" value="Submit" ></label></form> 

Database:

Code: Select all

 
$day = date('d');
$month = date('m');
$year = date('Y');
$h = date('G');
$m = date('i');
mysql_select_db (DB_NAME);
if (!$dbc)
  {
  die('Could not connect: ' . mysql_error());
  }
  $sql="INSERT INTO nimtazblog (title, text, author, day, month, year, h, m)
VALUES ('$_POST[title]','$_POST[text]','$_POST[author]', $day, $month, $year, $h, $m)";
if (!mysql_query($sql,$dbc))
  {
  die('Error: ' . mysql_error());
  }
;mysql_close($dbc)
 
 

then the data is saved into the databased and there it is displayed in separate lines.

When I try to get the data from the database but It then prints my text field in a signle line instead of printing it in multiple lines.
This is my code:

Code: Select all

 
$query  = "SELECT * FROM nimtazblog ORDER BY entryid DESC ";
            
$result = mysql_query($query);
            
$entryid=$row['entryid'];
            
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<h2 style='margin-bottom:-10px; margin-top:15px; color:#c993be'>{$row['title']} </h2>";
echo "<p style='line-height:10px; color:#813f74'>posted by {$row['author']} on {$row['day']}/{$row['month']}/{$row['year']}, at {$row['h']}:{$row['m']} </p>" ;
            
echo " <p class='txt' style='line-height:10px;'>{$row[$text]}<br>";     
 
 
 
I've read of the nl2br so I've tried assigning my row result for the text field to a variable 
 
$text=$row['text'];
;

and then try to print my results this way:

echo " <p class='txt' style='line-height:10px;'>"nl2br($text)"<br>";

but I am not getting it right..

Do you guys know what I'm doing wrong?
Thanks again!
Morena
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: I need a line break when extracting some text from a MySQL d

Post by mickeyunderscore »

Did you use this code exactly?

Code: Select all

echo " <p class='txt' style='line-height:10px;'>"nl2br($text)"<br>";
Because it should be: (though I would have expected the above to cause a parse error)

Code: Select all

echo "<p class='txt' style='line-height:10px;'>" . nl2br($text) . "<br>";
BTW are you absolutely sure the newline characters are being saved in the database? When it is output all on one line, have you tried right-clicking and viewing source to check the are there?
morena84
Forum Newbie
Posts: 9
Joined: Thu Apr 03, 2008 9:52 am

Re: I need a line break when extracting some text from a MySQL d

Post by morena84 »

Nope, not working :(
I can clearly see separate lines in the database. Am I storing it in the wrong data type field? At the moment it is in a TEXT field..
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: I need a line break when extracting some text from a MySQL d

Post by watson516 »

nl2br() should work. It replaces all new line characters in your text with a <br />, html's line break

Try putting the text in <pre>...</pre> tags. If it still doesn't work, theres a problem.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: I need a line break when extracting some text from a MySQL d

Post by Skoalbasher »

watson516 wrote:nl2br() should work. It replaces all new line characters in your text with a <br />, html's line break

Try putting the text in <pre>...</pre> tags. If it still doesn't work, theres a problem.
Quick question in this. So if a user hits "enter" on their keyboard in a textfield (like the one i'm typing in right now), that doesn't show for me in MYSQL. Does it save that information anywhere? So then I can use nl2br() to pull back out the carriage returns when echoing?
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: I need a line break when extracting some text from a MySQL d

Post by mickeyunderscore »

morena84 wrote:Nope, not working :(
I can clearly see separate lines in the database. Am I storing it in the wrong data type field? At the moment it is in a TEXT field..
To be honest this is quite confusing as I have a very similar concept (a notes system which is part of some diary functionality) and the code is pretty much the same as yours, except mine works.

You should make sure that the new line characters are being brought out of the database intact. Inspect the generated HTML source in your browser to make sure they are there
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: I need a line break when extracting some text from a MySQL d

Post by watson516 »

Skoalbasher wrote:
watson516 wrote:nl2br() should work. It replaces all new line characters in your text with a <br />, html's line break

Try putting the text in <pre>...</pre> tags. If it still doesn't work, theres a problem.
Quick question in this. So if a user hits "enter" on their keyboard in a textfield (like the one i'm typing in right now), that doesn't show for me in MYSQL. Does it save that information anywhere? So then I can use nl2br() to pull back out the carriage returns when echoing?
I have only used the nl2br function with information stored in a TEXT column. Each time when I go and check it out in phpmyadmin, the new line is always visible. If its not and theres no \n or anything, then i doubt its there.
morena84
Forum Newbie
Posts: 9
Joined: Thu Apr 03, 2008 9:52 am

Re: I need a line break when extracting some text from a MySQL d

Post by morena84 »

I'm not entiry sure I understood your question..no sign of a line break is stored in the database.
Except that the "enter" hit on the keyboard is reflected in the database because I can see separate lines in my mysql admin table.
Post Reply