HTML
|
|
|
|
<?
class Thread { var $beginLevel = "<ul>";
var $endLevel = "</ul>";
var $beginItem = "<li>";
var $endItem = "</li>";
var $wholeThread;
function Thread($code="")
{ if(!empty($code))
{ $this->beginLevel = $code[beginLevel];
$this->endLevel = $code[endLevel];
$this->beginItem = $code[beginItem];
$this->endItem = $code[endItem];
}
}
function sortChilds($threads)
{ while(list($var, $value) = each($threads))
$childs[$value[parent]][$value[ID]] = $value;
return $childs;
}
function convertToThread($threads, $thread)
{ $this->wholeThread .= $this->beginLevel;
while(list($parent, $value) = each($thread))
{ $this->wholeThread .= $this->beginItem . $value[content] . $this->endItem;
if($threads[$parent] && !$value[closed])
$this->convertToThread($threads, $threads[$parent]);
}
$this->wholeThread .= $this->endLevel;
return $this->wholeThread;
}
}
?>
|
|
|
Usage Example
|
//The array with data to be printed, this has to contain an ID, a parent and a content item...closed is optional (if true, the thread will be printed collapsed)
$threads = array(array("ID" => 1, "content" => "item1", "parent" => 0, "closed" => false),
array("ID" => 2, "content" => "subitem1", "parent" => 1, "closed" => false),
array("ID" => 3, "content" => "subsubitem1", "parent" => 1, "closed" => false),
array("ID" => 4, "content" => "item2", "parent" => 0, "closed" => false),
array("ID" => 5, "content" => "subitem2", "parent" => 4, "closed" => false),
array("ID" => 6, "content" => "subsubitem2", "parent" => 5, "closed" => false));
//The parameter array with the 'beginLevel', 'endLevel', 'beginItem' and 'endItem' hashitems; This will determine how the threads will be printed
//just flat beneath eachother
$flat = array("beginLevel" => "", "endLevel" => "", "beginItem" => "", "endItem" => "<br>");
//weird, just an example to show what can be done :)
$flat2 = array("beginLevel" => "<TABLE BORDER=1><TR><td>", "endLevel" => "</td></tr></table>", "beginItem" => "", "endItem" => "");
$myThread = new Thread(); //With no parameter the threads will be threaded
//$myThread = new Thread($flat); // This will print it flat
//$myThread = new Thread($flat2); //This will print it funky :)
$converted = $myThread->sortChilds($threads); //sort threads by parent
print $myThread->convertToThread($converted, $converted[0]); //print the threads
|
|
|
Rate This Script
|
|
|
|