Hey everyone. Well I got my little news code pulling my posts from my DB ok and organizing them well with the next and previous page buttons but I'm having trouble with making it so if theres no posts it will say "no posts available". When I try to put the else statement in the code it just errors. Could someone please take a look at my code and possibly show me what to do? Thanks!
<?
$server = "localhost";
$user = "xxx";
$pass = "xxx";
$databasename = "xxx";
$db = mysql_connect($server, $user, $pass);
mysql_select_db($databasename,$db);
$sql = "SELECT * FROM news ORDER BY posted DESC ";
$query = mysql_query($sql,$db);
$total_results = mysql_num_rows($query);
$limit = "2"; //limit of archived results per page.
$total_pages = ceil($total_results / $limit); //total number of pages
if (empty($page))
{
$page = "1"; //default page if none is selected
}
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB
$query = "SELECT * FROM news ORDER BY posted DESC LIMIT $offset, $limit";
$result = mysql_query($query);
//This is the start of the normal results...
{
// Initialise variable to hold news items
$newsText = "";
// For each news item returned from query...
while($row = mysql_fetch_array($result))
{
// Format date in 'mm/dd/yy' format
$posted = strftime("%m.%d.%y", $row['posted']);
// Add title to output
$newsText .= '<font class="blureg">';
$newsText .= $posted . "/ ";
$newsText .= '</font> <font class="gryreg">';
$newsText .= $row['title'];
$newsText .= '</font><br>';
// Add news item body with a double linebreak
$newsText .= '<font class="blkreg">';
$newsText .= stripslashes($row['body']);
$newsText .= '</font><br><br>';
// EMAIL PARSER
$pattern = '/\[(email|EMAIL)=(.*?)\](.*?)\[\/(EMAIL|email)\]/';
$replace = '<a href="mailto:\\2">\\3</a>';
$newsText = preg_replace($pattern, $replace, $newsText);
// URL PARSER
$pattern = '/\[(url|URL)=(.*?)\](.*?)\[\/(URL|url)\]/';
$replace = '<a href="\\2" target="_blank">\\3</a>';
$newsText = preg_replace($pattern, $replace, $newsText);
// NEWLINE PARSER
$newsText = ereg_replace("\r\n", "<br>", $newsText);
}
// Output news items
print ($newsText);
}
mysql_close();
// This is the Previous/Next Navigation
echo "<font class=gryreg>Page $page of $total_pages</font>"; // total pages
if ($page != 1)
{
$prevpage = $page - 1;
echo " <a href=$PHP_SELF?page=$prevpage><<-- </a> "; // Previous Page Link
}
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo " <a href=$PHP_SELF?page=$nextpage> -->></a>"; // Next Page Link
}
// This is the end of the Previous/Next Navigation
?>