MySQL Database Php

$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 = '';
if ($depth == 0) {
// Clear $contlines[]
unset($contlines);
}
for ($i = 0; $i < $depth; $i++) {
$tmpstr .= '';
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 .= '';
} else {
$tmpstr .= '';
}
} elseif ($contlines[$i+1] == 1) {
$tmpstr .= '';
}
$tmpstr .= '';
$width -= $indent;
}
$tmpstr .= "";
if ($depth == 0) {
$tmpstr .= '
';
}
// 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 .= 'Subject: ' . $nodes[$id][0];
$tmpstr .= '
Sender: ' . $nodes[$id][1] .
'
';
$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 = '';
if ($depth == 0) {
// Clear $contlines[]
unset($contlines);
}
// Netscape seems not to like this...
// $tmpstr .= "";
$tmpstr .= "";
if ($depth == 0) {
$tmpstr .= '
';
}
$tmpstr .= 'Subject: ' . $nodes[$id][0];
$tmpstr .= '
Sender: ' . $nodes[$id][1] .
'
';
for ($i = $depth; $i > 0; $i--) {
$tmpstr .= '';
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 .= '';
} else {
$tmpstr .= '';
}
} elseif ($contlines[$i] == 1) {
$tmpstr .= '';
}
$tmpstr .= '';
}
$tmpstr .= '';
$table = $tmpstr . $table;
$prevdepth = $depth;
}
return $table;
}
echo "";
echo tableshow($threadlist, $maxdepth, $nodes, $tablewidth, $indentlevel);
echo "
\n";
echo "

This might be better if you happen to speak hebrew or
arabic...

";
echo "";
echo tablerevshow($threadlist, $maxdepth, $nodes, $tablewidth, $indentlevel);
echo "
\n";
?>