After adding my blog as a news feed last week, I decided it would be much tidier to restrict the number of blog entries on each page . This should reduce time to render the page, and most blog entries that are read will be the more recent ones anyway. The inclusion of Next/Previous buttons would still allow the reader to navigate through other blog posts.
Clearly, the page request would have to be processed seperately for each batch of posts, so this requires some changes to the page that calls the underlying function. The new inline page PHP code looks like this:
The number of entries to be show per page is 5 and by casting the index to an integer, if it isnt passed through by the page, we default it to zero.
Next Up is the function itself, the doShowBlogSections call does this:
function doShowBlogSections($blogURL, $blogEntryIndex, $nBlogEntries) {
// get rss feed contents
$library = @simplexml_load_file($blogURL);
$i=0;
// loop over each entry and output if relevant and possible
$needNextButton = false;
foreach ($library->entry as $entry) {
// only show nBlogEntries...if we exit the loop normally, no next button required
if ( ($i) == ($nBlogEntries+$blogEntryIndex) ) {
$needNextButton = true; // having to bail - there are more entries available
break;
}
// only output blog entries from blogEntryIndex onwards
if ($i >= $blogEntryIndex ) {
echo "<p>";
echo "Title: ".$entry->title."<br />\n";
$pubDate = $entry->published;
// sort out obscure date format used by blogger.com
if ( $pubDate = strtotime($pubDate) ) {
// we have a valid unix timestamp value from the date string
$pubDate = date("F j, Y, g:i a", $pubDate);
echo "Published on ".$pubDate."</p>\n";
}
else {
// give up and just output in whatever format the date/time is in
echo "Published on ".$entry->published."</p>\n";
}
echo "<div>".$entry->content."</div>\n";
}
$i++;
}
// Provide suitable Next and Previous buttons
if ( $blogEntryIndex == 0 ) $needPreviousButton = false;
else $needPreviousButton = true;
$blogPreviousIndex = $blogEntryIndex - 5;
if ($blogPreviousIndex < 0 ) $blogPreviousIndex = 0;
$blogNextIndex = $i;
$thisPage = $_SERVER['PHP_SELF']; // need to be self referencing for page name
echo "<p>";
if ($needPreviousButton) echo "<a href=".$thisPage."?index=".$blogPreviousIndex."><<< Previous</a>";
if ($needPreviousButton && $needNextButton) echo ' ';
if ($needNextButton) echo "<a href=".$thisPage."?index=".$blogNextIndex.">Next >>></a>";
echo "</p>";
}
It should be fairly self explanatory as the code has a liberal sprinkling of comments. Other enhancements that I may add at a a later date include:
Clearly, the page request would have to be processed seperately for each batch of posts, so this requires some changes to the page that calls the underlying function. The new inline page PHP code looks like this:
$nEntriesPerPage = 5;
$blogIndex = (int) $_GET["index"];
$foo = doShowBlogSections($blogs[0], $blogIndex, $nEntriesPerPage);
$blogIndex = (int) $_GET["index"];
$foo = doShowBlogSections($blogs[0], $blogIndex, $nEntriesPerPage);
The number of entries to be show per page is 5 and by casting the index to an integer, if it isnt passed through by the page, we default it to zero.
Next Up is the function itself, the doShowBlogSections call does this:
function doShowBlogSections($blogURL, $blogEntryIndex, $nBlogEntries) {
// get rss feed contents
$library = @simplexml_load_file($blogURL);
$i=0;
// loop over each entry and output if relevant and possible
$needNextButton = false;
foreach ($library->entry as $entry) {
// only show nBlogEntries...if we exit the loop normally, no next button required
if ( ($i) == ($nBlogEntries+$blogEntryIndex) ) {
$needNextButton = true; // having to bail - there are more entries available
break;
}
// only output blog entries from blogEntryIndex onwards
if ($i >= $blogEntryIndex ) {
echo "<p>";
echo "Title: ".$entry->title."<br />\n";
$pubDate = $entry->published;
// sort out obscure date format used by blogger.com
if ( $pubDate = strtotime($pubDate) ) {
// we have a valid unix timestamp value from the date string
$pubDate = date("F j, Y, g:i a", $pubDate);
echo "Published on ".$pubDate."</p>\n";
}
else {
// give up and just output in whatever format the date/time is in
echo "Published on ".$entry->published."</p>\n";
}
echo "<div>".$entry->content."</div>\n";
}
$i++;
}
// Provide suitable Next and Previous buttons
if ( $blogEntryIndex == 0 ) $needPreviousButton = false;
else $needPreviousButton = true;
$blogPreviousIndex = $blogEntryIndex - 5;
if ($blogPreviousIndex < 0 ) $blogPreviousIndex = 0;
$blogNextIndex = $i;
$thisPage = $_SERVER['PHP_SELF']; // need to be self referencing for page name
echo "<p>";
if ($needPreviousButton) echo "<a href=".$thisPage."?index=".$blogPreviousIndex."><<< Previous</a>";
if ($needPreviousButton && $needNextButton) echo ' ';
if ($needNextButton) echo "<a href=".$thisPage."?index=".$blogNextIndex.">Next >>></a>";
echo "</p>";
}
It should be fairly self explanatory as the code has a liberal sprinkling of comments. Other enhancements that I may add at a a later date include:
- First and Last buttons
- Some indication of total number of log entries and where you are in that list
- a list of blog entry titles and dates as clickable links
- a link to the comment page