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:

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

$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:

$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

    you echo syntax is wrong. change to:

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

      a simple example:

      $text = '
      this text
      has line breaks
      in it
      ';
      
      echo '<p style="line-height: 20px;">' . nl2br($text) . '</p>';
      

        But I don't know why it's not extracting the data from the database in that format..Am I storing it in the wrong data type field? At the moment it is in a TEXT field..

          It's extracting the data from the database exactly as it was stored. So, if you stored this:

          this
          this text
          has line breaks
          in it

          then no, of course you won't see any line breaks when its displayed in your browser. Why? Because your browser doesn't care where line breaks are in the HTML source code; it only cares where the <br> elements are (as far as displaying lines of text).

            does that mean that I have to insert line breaks when inserting the data in the database? isn't there an automatic function I can use?

            I was only addin the line break when I was extracting because that's the advise I've read online..

              if your data is stored with line breaks then [man]nl2br[/man] will simply convert them to <br> tags

                Write a Reply...