You've seen how many Web sites (including this one) allow you topost comments and opinions on the material they publish. If you've everwished you had this capability on your Web site, or are simply curious asto how this is accomplished, read on.
The final piece of the puzzle is the script "details.php", which uses the values passed to it on the URL (article, section and comment identifiers) to query the database and return a single page containing the author's name, the time at which the comment was posted, and the body of the comment. In case the author has included an email address, it is placed within a mailto: hyperlink and printed as well.
<html>
<head>
<basefont face="Verdana, Arial">
</head>
<body>
<font face="Verdana, Arial" size="4" color="#66CC00">
Mole In A Hole
</font>
<p>
<font face="Verdana, Arial" size="2" color="Black">
Some things just don't change - at thirty, Adrian Mole is still the
whining, unfulfilled loser we first encountered at age 13. He's still
writing to the BBC, he's still madly in love with Pandora, now a
high-profile politician in Blair's New Britannia, his family is still as
dysfunctional as ever. More in our exclusive review of the new Sue Townsend
novel.
<p>
<a href="post.php?section=<? echo $section; ?>&article=<? echo $article;
?>">Comment</a> on this article.
<p>
<hr>
<?
include("config.php");
include("common.php");
// connect and get this post
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to
connect!");
$query = "SELECT username, email, subject, post, timestamp from $table
WHERE id = $id";
$result = mysql_db_query($database, $query, $connection) or die ("Error in
query: $query. " . mysql_error());
// display post content
list($username, $email, $subject, $post, $timestamp) =
mysql_fetch_row($result);
// if email address available, place it in a mailto:
if ($email) { $email = "<<a href=mailto:" . $email . ">" . $email .
"</a>>"; }
echo "<font face=verdana size=2 color=black>Posted by <b>$username</b>
$email on " . formatDate($timestamp) . "<br>";
echo "<b>Subject</b>: $subject<br>";
echo "<b>Comment:</b> " . nl2br($post) . "</font><p>";
?>
<hr>
<a href="post.php?replytopost=<? echo $id; ?>§ion=<? echo $section;
?>&article=<? echo $article; ?>">Reply</a> to this post or
<a href="list.php?section=<? echo $section; ?>&article=<? echo $article;
?>">read more comments</a>
</font>
</body>
</html>
Here's what it looks like:
Pay special attention to the "reply" link at the bottom of the post - in case a reader wishes to reply to this specific post, the $replytopost variable holds the name of the post that is being replied to. This $replytopost variable is then transferred to "post.php" as an additional parameter and stored in the database, thereby completing the loop required to generate the discussion tree.
The formatDate() function is used to convert the 14-digit mySQL timestamp into something a little more readable.
// format the date correctly
function formatDate($val)
{
// split up the timestamp
$year=substr($val,0,4);
$month=substr($val,4,2);
$day=substr($val,6,2);
$hh=substr($val,8,2);
$mm=substr($val,10,2);
// convert it into a standard timestamp and format it
$date = date("d M Y H:i", mktime($hh, $mm, 0, $month, $day, $year));
return $date;
}
This
article copyright Melonfire 2001. All rights reserved.