2011-07-04 11 views
5

मैं, this one की तरह एक इटरेटर बनाने के लिए कोशिश कर रहा हूँ टिप्पणियों की सूची के लिए में बहु स्तरीय "इटरेटर" को लागू करने:पीएचपी

// 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; ?> 

तो सब कुछ ठीक काम कर रहा है, एक चीज़ के अलावा: मैं यह समझ नहीं सकता कि एन को कैसे संसाधित किया जाए दिलचस्पी टिप्पणी :(

$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> 'से पहले उन्हें डालने की आवश्यकता उनके माता-पिता की टिप्पणियों के ...

कोई विचार?

उत्तर

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> प्रति गहराई नीचे डालना।

बच्चों को कैसे पार किया जाता है, इस क्रम को नियंत्रित करने के लिए झंडे का उपयोग करें।