Your gonna have to use recursion dude. If you don't know what that is try looking it up. Basically you have to track the parent message in each child record. Here is some code which works for expanding a thread. The database code is handled in a class I wrote, so you will have to sub it out for mysql code or whatever db code you use.
To show a whole board stick this function in a loop which goes through all the top threads (in my case all threads with a parent of 0)
function expand($PID,$string,$curr,$top)
{
global $virtual_path,$url,$color,$ID;
if ($color=="#FFFFFF")
$color="#EFEBF7";
else
$color="#FFFFFF";
global $open,$PHP_SELF,$retract;
$RS=Query("select Quick.ID,Quick.Rejection,Quick.Subject,Employee.Name,Quick.Date from Quick,".
"Employee where Employee.ID=Quick.Employee_ID and Quick.ID=$PID and Projects_ID=$ID;");
echo "<tr bgcolor=$color><td width=70%>";
echo $string." • <a href='$PHP_SELF?Action=ShowMessage&$url&PID=".$RS->get("ID")."'><u>";
if ($top==$RS->get("ID"))
echo "<strong>";
echo $RS->get("Subject");
if ($top==$RS->get("ID"))
echo "</strong>";
echo "</u></a></td></tr>";
$RS2=Query("select * from Quick where Master=$PID;");
//echo "select * from span where master=$ID;";
while (!($RS2->eof()))
{
//echo "\n calling expan with $row->ID and indent of ".($indent+1)."\n";
//alert($string);
if ($PID!=$curr)
{
$string.="<img src='$virtual_path/images/spacer.gif' alt='spacer.gif' border=0 height=4 width=25>";
$curr=$PID;
}
expand($RS2->get("ID"),$string,$curr,$top);
$RS2->MoveNext();
}
}