PHP/mySQL help needed [solved] (Full Version)

All Forums >> [Web Development] >> ASP and Database



Message


Tailslide -> PHP/mySQL help needed [solved] (10/27/2005 6:27:42)

(oooh - scary! I'm not often found in the rarified atmosphere of this forum so be nice to me!!)

I'm trying to get to grips with PHP/mySQL to create a news archive for a client. I've got it all working nicely including an on-line form for them to add a new story.

My problem is that when the news items are displayed the line breaks aren't preserved. I've checked the actual DB itself in PHPAdmin and the line breaks are there - they just don't show up when the story is called.

I've been adding various different bits of code to try to keep the line breaks where they should be but nothing seems to make any difference. I'd appreciate any suggestions you might have to help.

The PHP on the page which reproduces the news stories is:

<?php

    $dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("mydatabase");
 			
    // get the id from the URL request
    $id = $_REQUEST['id'];

    // retrieve the row from the database
    $query = "SELECT * FROM archive WHERE id='$id'";
   
    $result = mysql_query( $query );

    // print out the results
    if( $result && $newsitem = mysql_fetch_object( $result ) )
    {
      // print out the info
      $newsdate = $newsitem -> newsdate;
      $newssummary = $newsitem -> newssummary;
      $newstext = $newsitem -> newstext;

      echo '<p>' . $newsdate . '<br />' . '<strong>' . 
			$newssummary . '</strong>' . '<br />' . $newstext . '</p>';
			}
			
			// special characters
			$newssummary = htmlspecialchars($newssummary);
			$newstext = htmlspecialchars($newstext);
      			
			// hyperlinks
			$newstext = eregi_replace (
			 '\\[L]([-_./a-z0-9!&%#?+,\'=:;@~]+)\\[EL]',
			 '<a href="\\1">\\2</a>', $newstext);

                       // added this bit in to create line breaks
                      function br2nl($str) {
                      $str = preg_replace("/(\r\n|\n|\r)/", "", $str);
                      return preg_replace("=<br */?>=i", "\n", $str);
                      }

?>





Edit: Finally figured it out: used
$newstext = nl2br($newstext); 
and it works nicely now.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.0625