<?php
// this function threads comments so that you can trace a comment
// "conversation".
function thread($post_id, $parent_id) {
$commentlist = mysql_query("SELECT * FROM comments WHERE entry=$post_id and parent=$parent_id;");
// if we have comments to display, display them, if not, exit.
if ($commentlist != NULL) {
echo "<ul>"; // the <ul> is what creates the indent
while ($myrow = mysql_fetch_array($commentlist)) {
echo "<!-- comment ".$myrow["id"]." -->\n<li><h2>Comment posted by ".$myrow["poster"]."</h2><p>".$myrow["comment"]."</p><p><a href='newcomment.php?post=".$post_id."&parent=".$myrow["id"]."'>Reply to this comment</a></p></li>\n";
thread($post_id, $myrow["id"]); // run through
// the function again, this is what creates the
// threading effect
}
echo "</ul>";
}
}
// check we have a valid post to display comments for
$post = $_GET["post"];
if ($post != "" && is_numeric($post)) {
include "db.inc";
$result2 = mysql_query("SELECT title, entry FROM entries where id=$post");
$entry = mysql_fetch_array($result2);
// if the entry requested exists, display it, then display the
// comments
if ($entry != NULL) {
echo "<h1>".$entry["title"]."</h1>\n<p>".$entry["entry"]."</p>\n<a href='newcomment.php?post=$post'>Comment on this entry</a>\n<hr />\n";
thread($post, 0);
// if the post doesn't exist, say so
} else {
echo "This post appears not to exist, sorry.";
}
// if the post reference was invalid for any reason, say so
} else {
echo "No post reference was supplied or post reference was invalid.";
}
?>