10.4. Displaying Posts

I've written the following code and saved it as public_files/view.php to display the blog posts. Posts are ordered in descending date order so newer posts appear first. The current month's posts are displayed first and links are placed at the bottom of the page to traverse other months.

I can pass a timestamp as a parameter to display various months, just like was done in the calendar application in Chapter 5. If the requested month isn't within reason or if the parameters weren't provided, the current month and year are assumed. The script can then determine whichever information it needs from the timestamp using PHP's date() function.

$timestamp = (isset($_GET['t'])) ? $_GET['t'] : time();
list($day, $year) = explode('/', date('m/Y', $timestamp));

After displaying the contents of the month's posts, previous and next links can be generated to traverse to other months. Again, as in Chapter 5, the timestamp can be easily be modified using strtotime(). Although you don't want to provide links beyond the oldest and newest dates, you should check the dates of the oldest and newest entries stored in the database.

$query = sprintf('SELECT UNIX_TIMESTAMP(POST_DATE) AS POST_DATE ' .
     'FROM %sBLOG_POST ORDER BY POST_DATE DESC',
     DB_TBL_PREFIX);
$result = mysql_query($query, $GLOBALS['DB']);
if (mysql_num_rows($result))
{

    // determine date of newest post
    $row = mysql_fetch_assoc($result);
    $newest = $row['POST_DATE'];

    // determine date of oldest post
    mysql_data_seek($result, mysql_num_rows($result) − 1);
    $row = mysql_fetch_assoc($result);
    $oldest = $row['POST_DATE'];

    if ($timestamp > $oldest)
    {
        echo '<a href="' . htmlspecialchars($_SERVER['PHP_SELF']) .
            '?t=' . strtotime('−1 month', $timestamp) . '">Prev</a> ';
    }

    if ($timestamp < $newest)
    {
        echo ' <a href="' . htmlspecialchars($_SERVER['PHP_SELF']) .
            '?t=' . strtotime('+1 month', $timestamp) . '">Next</a>';
    }

}
mysql_free_result($result);

Here is the full code for public_files/view.php:

<?php
// include shared code
include '../lib/common.php';
include '../lib/db.php';

// determine current viewed month and year
$timestamp = (isset($_GET['t'])) ? $_GET['t'] : time();
list($day, $year) = explode('/', date('m/Y', $timestamp));

// retrieve entries for currently viewed month
$query = sprintf('
    SELECT
        POST_ID, POST_TITLE, POST_TEXT,
        UNIX_TIMESTAMP(POST_DATE) AS POST_DATE
    FROM
        %sBLOG_POST
    WHERE
        DATE(POST_DATE) BETWEEN
            "%d-%02d-01" AND
            DATE("%d-%02d-01") + INTERVAL 1 MONTH - INTERVAL 1 DAY
    ORDER BY
        POST_DATE DESC',
    DB_TBL_PREFIX,
    $year,  $month,
    $year, $month);
$result = mysql_query($query, $GLOBALS['DB']);

ob_start();
while ($record = mysql_fetch_assoc($result))
{
    echo '<h2>' . $record['POST_TITLE'] . '</h2>';
    echo '<p>' . date('m/d/Y', $record['POST_DATE']) . '</p>';
    echo $record['POST_TEXT'];
    echo '<hr/>';
}
mysql_free_result($result);

// generate link to view previous month if appropriate
$query = sprintf('SELECT UNIX_TIMESTAMP(POST_DATE) AS POST_DATE ' .
     'FROM %sBLOG_POST ORDER BY POST_DATE DESC',
     DB_TBL_PREFIX);
$result = mysql_query($query, $GLOBALS['DB']);
if (mysql_num_rows($result))
{

    // determine date of newest post
    $row = mysql_fetch_assoc($result);
    $newest = $row['POST_DATE'];

    // determine date of oldest post
    mysql_data_seek($result, mysql_num_rows($result) - 1);
    $row = mysql_fetch_assoc($result);

$oldest = $row['POST_DATE'];

    if ($timestamp > $oldest)
    {
        echo '<a href="' . htmlspecialchars($_SERVER['PHP_SELF']) .
            '?t=' . strtotime('−1 month', $timestamp) . '">Prev</a> ';
    }

    if ($timestamp < $newest)
    {
        echo ' <a href="' . htmlspecialchars($_SERVER['PHP_SELF']) .
            '?t=' . strtotime('+1 month', $timestamp) . '">Next</a>';
    }

}
mysql_free_result($result);

// link to RSS feed
$GLOBALS['TEMPLATE']['head_extra'] = '<link rel="alternate" ' .
    'type="application/rss+xml" href="rss.php" title="My Blog">';

echo '<p><a href="rss.php">RSS Feed</a></p>';

$GLOBALS['TEMPLATE']['content'] = ob_get_clean();


include '../templates/template-page.php';
mysql_close($GLOBALS['DB']);
?>

Figure 10-3 shows the blog posts for a given month.

Figure 10-3. Figure 10-3

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.83.182