在PHP

2011-07-04 9 views
5

我试图创建一个迭代器像this one,为的评论列表,实现多层次的“迭代”:在PHP

// the iterator class, pretty much the same as the one from the php docs... 
abstract class MyIterator implements Iterator{ 

    public $position = 0, 
     $list; 

    public function __construct($list) { 
    $this->list = $list; 
    $this->position = 0; 
    } 

    public function rewind() { 
    $this->position = 0; 
    } 

    public function current() { 
    return $this->list[$this->position]; 
    } 

    public function key() { 
    return $this->position; 
    } 

    public function next() { 
    ++$this->position; 
    } 

    public function valid() { 
    return isset($this->list[$this->position]); 
    } 
} 

注释迭代器:

class MyCommentIterator extends MyIterator{ 

    public function current(){ 
    return new Comment($this->list[$this->position]); 
    }  
} 

这是我如何使用它:

$comments = GetComments(); // gets the comments from the db 
if($comments): ?> 

    <ol> 
    <?php foreach(new MyCommentIterator($comments) as $comment): ?> 
    <li> 
     <p class="author"><?php echo $comment->author(); ?></p> 

     <div class="content"> 
     <?php echo $comment->content(); ?> 
     </div> 

     <!-- check for child comments and display them --> 

    </li> 
    <?php endforeach; ?> 
    </ol> 

<?php endif; ?> 

所以一切工作正常,除了一两件事:我无法弄清楚如何处理N个相关捐资评论:(

$comments阵列返回注释平面列表,如:

[0] => object(
    'id' => 346, 
    'parent' => 0, // top level comment 
    'author' => 'John', 
    'content' => 'bla bla'   
), 

[1] => object(
    'id' => 478, 
    'parent' => 346, // child comment of the comment with id =346 
    'author' => 'John', 
    'content' => 'bla bla'   
) 
... 

我需要以某种方式能够检查孩子的意见(多层次)和</li>'之前将它们插入s的父母的意见...

任何想法?

回答

1

您正在使用扁平数组,但实际上,该数组的项是树或分层数据结构。

你基本上正在显示一个顺序列表。也许你应该首先构建树/分层数据结构,而不显示,并且稍后显示树列表中的数据。

/* array */ function FlatArrayToTreeArray(/* array */ $MyFlatArray) 
{ 
    ... 
} 

/* void */ function IterateTree(/* array */ $MyTreeArray) 
{ 
    ... 
} 

/* void */ function Example() { 
    $MyFlatArray = Array(
    0 => object(
     'id' => 346, 
     'parent' => 0, // top level comment 
     'author' => 'John', 
     'title' => 'Your restaurant food its too spicy', 
     'content' => 'bla bla'   
    ), 
    1 => object(
     'id' => 478, 
     'parent' => 346, // child comment of the comment with id =346 
     'author' => 'Mike', 
     'title' => 'Re: Your restaurant food its too spicy', 
     'content' => 'bla bla'   
    ), 
    2 => object(
     'id' => 479, 
     'parent' => 478, // child comment of the comment with id =346 
     'author' => 'John', 
     'title' => 'Re: Your restaurant food its too spicy', 
     'content' => 'bla bla'   
    ), 
    3 => object(
     'id' => 479, 
     'parent' => 346, // child comment of the comment with id =346 
     'author' => 'Jane', 
     'title' => 'Re: Your restaurant food its too spicy', 
     'content' => 'bla bla'   
    ) 
); 

    $MyTreeArray = FlatArrayToTreeArray($myflatarray); 

    IterateTree($MyTreeArray); 
} // function Example() 

干杯。

3

递归是你的朋友。

displaycomment(comment): 
    $html .= "<ol>" . comment->html; 
    foreach comment->child: 
     $html .= "<li>" . displaycomment(child) . "</li>"; 
    $html .= "</ol>"; 
    return $html; 

出现在这篇文章中所有的代码都是伪。与真实代码的任何相似之处,工作或破碎,纯属巧合。

3

你可能想看看RecursiveIterator InterfacePHP Manual。如果使用该接口的方法扩展迭代器,则可以依次使用RecursiveIteratorIteratorPHP Manual的实例迭代您的评论。

但是,由于您的输出是一个平面列表,您需要自行处理关卡的逻辑,每深度插入<ol>,每个深度插入</ol>

使用这些标志来控制孩子如何穿越的顺序。