After a lot of swearing, head-banging and generally petulant behaviour on my part I've finally sort of got my news archive working.
I've got a mysql db which the site owner can enter news items into from a form on their website.
The news item heading and date are displayed on one page and when clicked the heading takes the user to the full new item on a new page.
My main problem now is that the line breaks entered by the site owner when they enter an item in the submit form aren't preserved in the reproduced full article.
I tacked on a few lines of code I borrowed from a book which I thought might help but don't appear to do anything. I'd be really grateful if you could have a look at my code and offer any suggestions.
I checked the DB in phpadmin and the line breaks are preserved there - just not in the reproduced version.
Code: Select all
<?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);
// paragraphs and line breaks
$newstext = ereg_replace("\r\n", "\n", $newstext);
$newstext = ereg_replace("\r", "\n", $newstext);
$newstext = ereg_replace("\n\n", '</p><p>', $newstext);
$newstext = ereg_replace("\n", '<br />', $newstext);
// hyperlinks
$newstext = eregi_replace (
'\\[L]([-_./a-z0-9!&%#?+,\'=:;@~]+)\\[EL]',
'<a href="\\1">\\2</a>', $newstext);
?>