Hi. I'm trying to get the following code to display the most recent full blog entry on the index page. It currently displays the Archive page and Add Entry page fine, but not the index page. I've tried several different things, but just can't work out how to do it. Anybody have any suggestions. Thanks!
require ("db.php"); // establish a connection to the database
include ("tpl/header.html"); // include header template
$content = ""; // start empty
@$page = strip_tags($_GET['p']); // sanitize data.
// set it so that if page is empty (IE, home page) show archive posts.
//if (empty($page)) {
//$page = "archive";
//}
if ($page == 'add') {
// display the add form..
if (empty($_POST['addblog'])) {
$content .= "
<form action=\"{$_SERVER['PHP_SELF']}?p=add\" method=\"post\">
<label for=\"title\">Entry Title:</label> <input type=\"text\" name=\"title\" id=\"title\" /><br />
<label for=\"content\">Blog Entry:</label><br /><textarea name=\"content\" id=\"content\" rows=\"15\"></textarea><br />
<input type=\"submit\" name=\"addblog\" id=\"addblog\" value=\"Add Entry\" />
</form>
";
} else {
// sanitization is important. You could catch some nasty bug.
$title = mysql_real_escape_string(htmlspecialchars($_POST['title'], ENT_QUOTES));
$entry = mysql_real_escape_string(htmlspecialchars($_POST['content'], ENT_QUOTES));
// re enable line breaks.
$entry = str_replace(array("\\r\\n", "\\r", "\\n"), "<br />", $entry);
// if the title or content is empty, tell the user it's empty!!
if ((!empty($title)) && (!empty($entry))) {
mysql_query("INSERT INTO `tt_blog` (`id` ,`title` ,`content` ,`datetime` ,`userip`)VALUES (NULL , '{$title}', '{$entry}', NOW(), '{$_SERVER['REMOTE_ADDR']}');");
$content .= "Entry Added!";
} else {
$content .= "Error. Please fill out all fields.";
}
}
} else
if ($page == 'archive') {
// Loop through the archive
$getpostsq = mysql_query("SELECT * FROM `tt_blog` ORDER BY `tt_blog`.`datetime` DESC LIMIT 5"); //lists 5 most recent entries
while ($getposts = mysql_fetch_assoc($getpostsq)) {
$content .= "<div class=\"archive\">
<p><em>{$getposts['datetime']}</em> » <a href=\"index.php?p={$getposts['id']}\"><strong>{$getposts['title']}</strong></a></p></div>";
}
} else
if ((int)$page) {
$getpostq = mysql_query("SELECT * FROM `tt_blog` WHERE `id` = '$page'"); // select current ID from database
$getpost = mysql_fetch_assoc($getpostq); // put the data from the previous query into an array which we can pull from
$content .= "<h1>{$getpost['title']}</h1>
<em>{$getpost['datetime']}</em>
<blockquote>{$getpost['content']}</blockquote>";
} else {
$content .= "Error, unknown action.";
}
echo $content; // echo out the pre filled content variable.
include ("tpl/footer.html"); // include footer template
<header>
<nav>
<a href="index.php">Home</a>
<a href="index.php?p=add">Add Entry</a>
<a href="index.php?p=archive">Archive</a>
</nav>
</header>