<?php
include "db.inc"; // include our database settings
// if we're displaying a page that doesn't start with the latest entry,
// check where we're supposed to start and make sure it's numeric.
$start = $_GET["start"];
if ($start != "" && is_numeric($start)) {
$result = mysql_query("SELECT * FROM entries where id < $start order by id desc limit 0,10",$db);
$numposts = mysql_numrows($result);
// otherwise, just get the latest 10 posts.
} else {
$result = mysql_query("SELECT * FROM entries order by id desc limit 0,10");
$numposts = mysql_numrows($result);
}
// check we have entries to display.
if (mysql_numrows($result)!=0) {
while ($myrow = mysql_fetch_array($result)) {
$lastid=$myrow["id"]; // this value will be used to show
// at which point we came to the end of the recordset,
// to allow paging through posts.
$result2 = mysql_query("SELECT * FROM comments where entry=$lastid");
$numrows = mysql_numrows($result2); // this value tells
// us how many comments there are on a post, so we can
// display it to the user
// fix a grammatical bug, spotted by Pickwick.
if ($numrows == 1) {
// display the posts
echo "<!-- entry ".$myrow["id"]." -->\n<h1>".$myrow["title"]."</h1><p>".$myrow["entry"]."</p>\n<p><a href='comment.php?post=".$myrow["id"]."'>".$numrows." comment</a> | <a href='newcomment.php?post=".$myrow["id"]."'>Post a new comment</a></p>\n";
} else {
// display the posts with "comments" when
// there's more than one comment.
echo "<!-- entry ".$myrow["id"]." -->\n<h1>".$myrow["title"]."</h1><p>".$myrow["entry"]."</p>\n<p><a href='comment.php?post=".$myrow["id"]."'>".$numrows." comments</a> | <a href='newcomment.php?post=".$myrow["id"]."'>Post a new comment</a></p>\n";
}
}
// give a useful error message if we can't find the specified start post
} else {
echo "The start value you requested is not valid";
}
// if there are more than 10 posts in total, display a link to allow
// people to view the previous posts.
if ($numposts > 9 && $lastid>1) {
echo "<a href='index.php?start=".$lastid."'>Next 10 entries</a>";
}
?>