Mega Code Archive

 
Categories / Php / MySQL Database
 

How to thread a list of messages in database and show it in a treelike structure

<? $host = ""; $database = ""; $user = ""; $passwd = ""; $tablewidth = 580; // in pixels $indentlevel = 15; // in pixels function traversetree($threadlist, $id, $leaves, $curdepth, $maxdepth) { global $leafnodes; $maxdepth = ($curdepth > $maxdepth? $curdepth : $maxdepth); sort($leaves); for (reset($leaves); $leaf = current($leaves); next($leaves)) { $threadlist[$leaf] = $curdepth; if (is_array($leafnodes[$leaf])) { traversetree(&$threadlist, $leaf, $leafnodes[$leaf], $curdepth+1, &$maxdepth); } } } function thread($rootnodes, $threadlist, $maxdepth) { global $leafnodes; rsort($rootnodes); for (reset($rootnodes); $id = current($rootnodes); next($rootnodes)) { $threadlist[$id] = 0; if (is_array($leafnodes[$id])) { traversetree(&$threadlist, $id, $leafnodes[$id], 1, & $maxdepth); } } } $conn = mysql_connect("$host", "$user", "$passwd"); mysql_select_db("$database", $conn); // 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 $tmpstr .= '<font size=-1><b>Subject:</b> ' . $nodes[$id][0]; $tmpstr .= '<br><b>Sender:</b> ' . $nodes[$id][1] . '</font></td></tr>'; $table = $tmpstr . $table; $prevdepth = $depth; } return $table; } function tablerevshow (&$threadlist, &$maxdepth, &$nodes, $width, $indent) { // Traverse through $threadlist in reverse order and construct the table for (end($threadlist); $id = key($threadlist); prev($threadlist)) { $depth = $threadlist[$id]; $span = $maxdepth - $depth + 1; $pixels = $width - ($depth * $indent); $tmpstr = '<tr align=right>'; if ($depth == 0) { // Clear $contlines[] unset($contlines); } // Netscape seems not to like this... // $tmpstr .= "<td colspan=$span width=$pixels>"; $tmpstr .= "<td colspan=$span>"; if ($depth == 0) { $tmpstr .= '<hr>'; } $tmpstr .= '<font size=-1><b>Subject:</b> ' . $nodes[$id][0]; $tmpstr .= '<br><b>Sender:</b> ' . $nodes[$id][1] . '</font></td>'; for ($i = $depth; $i > 0; $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) { if ($depth == $prevdepth || ($contlines[$i] == 1 && $depth < $prevdepth)) { $tmpstr .= '<img src="contnode- rev.gif">'; } else { $tmpstr .= '<img src="lastnode- rev.gif">'; } } elseif ($contlines[$i] == 1) { $tmpstr .= '<img src="contline.gif">'; } $tmpstr .= '</td>'; } $tmpstr .= '</tr>'; $table = $tmpstr . $table; $prevdepth = $depth; } return $table; } echo "<table width=$tablewidth border=0 cellspacing=0 cellpadding=0>"; echo tableshow($threadlist, $maxdepth, $nodes, $tablewidth, $indentlevel); echo "</table>\n"; echo "<hr><h3>This might be better if you happen to speak hebrew or arabic...</h3>"; echo "<table width=$tablewidth border=0 cellspacing=0 cellpadding=0>"; echo tablerevshow($threadlist, $maxdepth, $nodes, $tablewidth, $indentlevel); echo "</table>\n"; ?>