// You should get the messages in the order they were put in the database.
// If id is an autoincremented field this will work, otherwise consider
// adding a timestamp and order by it.
$query = "select id, subject, poster, followupto from chatboard order by id";
$res = mysql_query($query);
while (list($id, $subject, $poster, $followupto) = mysql_fetch_row($res)) {
$parents[$id] = 1;
if (isset($parents[$followupto])) {
$leafnodes[$followupto][$id] = (int)$id;
} else {
$rootnodes[$id] = (int)$id;
}
$nodes[$id][0] = $subject;
$nodes[$id][1] = $poster;
$nodes[$id][2] = $followupto;
}
thread($rootnodes, &$threadlist, &$maxdepth);
function tableshow (&$threadlist, &$maxdepth, &$nodes, $tablewidth, $indent) {
// Traverse through $threadlist in reverse order and construct the table
for (end($threadlist); $id = key($threadlist); prev($threadlist)) {
$width = $tablewidth;
$depth = $threadlist[$id];
$span = $maxdepth - $depth + 1;
$tmpstr = '<tr>';
if ($depth == 0) {
// Clear $contlines[]
unset($contlines);
}
for ($i = 0; $i < $depth; $i++) {
$tmpstr .= '<td width=' . $indent . '>';
if ($depth > $prevdepth) {
if ($contlines) {
for ($j = $prevdepth+1; $j < $depth;
$j++) {
$contlines[$j] = 0;
}
}
$contlines[$prevdepth] = 1;
}
if ($i == $depth-1) {
if ($depth == $prevdepth ||
($contlines[$i+1] == 1 &&
$depth < $prevdepth)) {
$tmpstr .= '<img src="contnode.gif">';
} else {
$tmpstr .= '<img src="lastnode.gif">';
}
} elseif ($contlines[$i+1] == 1) {
$tmpstr .= '<img src="contline.gif">';
}
$tmpstr .= '</td>';
$width -= $indent;
}
$tmpstr .= "<td colspan=$span width=$width>";
if ($depth == 0) {
$tmpstr .= '<hr>';
}
// In a real program you would probably put a link here to a
page
// that can show the message and allow you to reply to it