Heya
I'm working on a comment system which needs to support multi level comments. Ideally, all comments exist in one table (currently named intra_www). The structure includes the column "responseto" which has the unique ID of the comments column "wwwID" as parent comment.
I did some googling yesterday and found a walker class, found here: http://php4every1.com/scripts/walker-class/
I tried to use it but I couldn't get it to work properly yet...
This is the current content of the table.
wwwID userID responseto timestamp content priority seenby
1 0 0 1269390529 Lokaal @ lol is nu onder actieve ontwik... 1
2 0 1 1269390629 Wicked! 3
3 0 0 1269450941 Just some lil' testing for goods' sakes 2
The idea is to get it sorted in this way, the numbers is the WWWID
- #3
- #1
- - #2
(sorted with newest response first)
However, using the walker class the result I get is merely
- #3
- #1
This is the relevant part of the script:
$sql = 'SELECT * FROM intra_www JOIN intra_users ON intra_www.userID=intra_users.userID ORDER BY intra_www.timestamp DESC';
$comments = new commentWalker($connection);
$comments->setParentField('responseto');
$comments->setIdField('wwwID');
echo $comments->loadResults($sql)->trace()->buildComments();
And the extender commentWalker (The original class is the same as on the website):
class commentWalker extends walker {
function buildComments($comments = null, $lv = 0)
{
if (is_null($comments))
$comments = $this->_traced;
if (count($comments) < 1) {
ob_start();
?>
There are no comments to display.
<?php
return ob_get_clean();
}
$lv += 1;
$commentHtml = '';
if ($lv <= 3)
$commentHtml .= '<ul>';
foreach ($comments as $comment) {
ob_start();
?>
<div class="commentWrap">
<p class="userTime"><?php echo $comment->self->firstname.' '.$comment->self->lastname; ?> wrote on <?php echo date('F, j Y', $comment->self->timestamp); ?>
in <?php echo date('g:i a', $comment->self->timestamp); ?>
</p>
<p class="message">
<?php echo $comment->self->content; ?>
</p>
<p>
<a href="http://tutoriali/multiLevelComments/post.php?id=<?php echo $comment->self->wwwID; ?>" class="replayLink">Reply</a>
</p>
</div>
<?php
$html = ob_get_clean();
if (!empty($comment->childs)) {
$commentHtml .= '<li>';
$commentHtml .= $html;
$commentHtml .= $this->buildComments($comment->childs, $lv);
$commentHtml .= '</li>';
}
else {
$commentHtml .= '<li>';
$commentHtml .= $html;
$commentHtml .= '</li>';
}
}
if ($lv <= 3)
$commentHtml .= '</ul>';
return $commentHtml;
}
}
Any hints into fixing this is greatly appreciated!
Or, if you have another, possibly better solution to order the data in the right way, thanks in advance!