I'd suggestion following table:
id (autoincrement unique)
parentid (0 = first post)
userid
title
message
date
to order the threads do this select:
$query = "SELECT * FROM threadtable ORDER BY ".$OrderField;
$result = mysql_query($query);
in this way you can order the list by any of the fields in the table with a GET & POST variable OrderField.
then do following:
while($resarr = mysql_fetch_array($result)) {
$threads[$resarr['parentid']][$resarr['id']] = array("Title" => $resarr['title'], "Date" => $resarr['date'], "Message" => $resarr['message'], "userid" => $resarr['userid']);
}
this will give you an associative array holding every thread and posts.
Doing some recursive programming give you the ability to print out these posts.
eg. to print out a list of every thread - and no post do following:
function printThread($arr, $id=0, $indent=0, $all=false, $depth=0){
if(!is_array($arr)) return;
foreach($arr[$id] as $key) {
for($i=0; $i<$indent; $indent) echo " ";
echo $arr[$id][$key]['title']."<br />";
if(is_set($arr[$key])) && ($depth-1 >= 0 || $all)) printThread($id, $arr[$key], $indent+4, $depth-1);
}
return;
}
//this will print every main thread:
printThread($threads);
//this will print every post in thread-id = 10 except the mainpost.
//since the first post won't print in the function printThread
//print it first:
echo $threads[0][10]['title']."<br />";
printThread($threads, 10, 4, true);
//indent=4 to since first post already has been printed.
hope it makes sense