2012-10-15 88 views
0

這是我使用的類:PHP遞歸迭代器類問題

class RecursiveCategoryIterator implements RecursiveIterator { 
    const ID_FIELD = 'cat_ID'; 
    const PARENT_FIELD = 'parent'; 

    private $_data; 
    private $_root; 
    private $_position = 0; 

    public function __construct(array $data, $root_id='0') { 
     $this->_data = $data; 
     $this->_root = $root_id; 
    } 

    public function valid() { 
     return isset($this->_data[$this->_root][$this->_position]); 
    } 

    public function hasChildren() { 
     $subid = $this->_data[$this->_root][$this->_position][self::ID_FIELD]; 
     return isset($this->_data[$subid]) 
      && is_array($this->_data[$subid]); 
    } 

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

    public function current() { 
     return $this->_data[$this->_root][$this->_position]; 
    } 

    public function getChildren() { 
     return new self($this->_data, 
      $this->_data[$this->_root][$this->_position][self::ID_FIELD]); 
    } 

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

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

    public static function createFromResult($result) { 
     $menu_array = array(); 
     while($row = mysql_fetch_assoc($result)) { 
      $menu_array[$row[self::PARENT_FIELD]][] = $row; 
     } 

     return new self($menu_array); 
    } 
} 

它完美的作品時,我取根類別..意思是,當第一場parentzero ..但如果我取之間類別樹..然後它失敗..因爲它只能當它到達第一陣列零..

例如數組:

RecursiveCategoryIterator Object 
(
    [_data:RecursiveCategoryIterator:private] => Array 
     (
      [1] => Array 
       (
        [0] => Array 
         (
          [cat_ID] => 27 
          [cat_name] => Local Guess Papers 
          [cat_nicename] => local-guess-paper 
          [parent] => 1 
          [post_count] => 5 
          [depth] => 0 
         ) 

       ) 

      [27] => Array 
       (
        [0] => Array 
         (
          [cat_ID] => 28 
          [cat_name] => Class IX 
          [cat_nicename] => class-9 
          [parent] => 27 
          [post_count] => 0 
          [depth] => 1 
         ) 

        [1] => Array 
         (
          [cat_ID] => 34 
          [cat_name] => Class X 
          [cat_nicename] => class-10 
          [parent] => 27 
          [post_count] => 0 
          [depth] => 1 
         ) 

        [2] => Array 
         (
          [cat_ID] => 40 
          [cat_name] => Class XI 
          [cat_nicename] => class-11 
          [parent] => 27 
          [post_count] => 0 
          [depth] => 1 
         ) 

        [3] => Array 
         (
          [cat_ID] => 46 
          [cat_name] => Class XII 
          [cat_nicename] => class-12 
          [parent] => 27 
          [post_count] => 0 
          [depth] => 1 
         ) 

       ) 

      [28] => Array 
       (
        [0] => Array 
         (
          [cat_ID] => 29 
          [cat_name] => Year 2010 
          [cat_nicename] => year-2010 
          [parent] => 28 
          [post_count] => 0 
          [depth] => 2 
         ) 

        [1] => Array 
         (
          [cat_ID] => 30 
          [cat_name] => Year 2007 
          [cat_nicename] => year-2007-guess 
          [parent] => 28 
          [post_count] => 4 
          [depth] => 2 
         ) 

        [2] => Array 
         (
          [cat_ID] => 31 
          [cat_name] => Year 2008 
          [cat_nicename] => year-2008-guess 
          [parent] => 28 
          [post_count] => 4 
          [depth] => 2 
         ) 

        [3] => Array 
         (
          [cat_ID] => 32 
          [cat_name] => Year 2009 
          [cat_nicename] => year-2009-guess 
          [parent] => 28 
          [post_count] => 2 
          [depth] => 2 
         ) 

        [4] => Array 
         (
          [cat_ID] => 33 
          [cat_name] => Year 2006 
          [cat_nicename] => year-2006-cbse-guess-paper 
          [parent] => 28 
          [post_count] => 3 
          [depth] => 2 
         ) 

       ) 

) 

    [_root:RecursiveCategoryIterator:private] => 0 
    [_position:RecursiveCategoryIterator:private] => 0 
) 

沒有返回任何東西,因爲first element is NOT zero

這裏是我可以使用這個類的功能:

$sql='some sql to retrive data'; 
$result = mysql_query($sql); 

$recurdata = RecursiveCategoryIterator::createFromResult($result); 

makecat($recurdata,'',''); 

function makecat($iterator, $parent_name,$category) { 
foreach($iterator as $row) { 
    echo(''.$row['cat_name'].''); 
if($iterator->hasChildren()) { 


    makecat($iterator->getChildren(), $row['cat_name'],$category); 

     } 
} 
} 

所以我想知道如何顯示數據時取出從樹和when first parent is not the zero的中間?

回答

0

我解決它自己..在這裏張貼的解決方案的情況下,有人想在未來使用這個:

當初始化類,我需要通過當前父ID,然後它會都準備好去。

$recurdata = RecursiveCategoryIterator::createFromResult($result,$current_parent_id); 

然後在遞歸類,作爲回報補充一點:

public static function createFromResult($result,$root_id) { 
     //$this->root_id=$current_cat_id; 
     $menu_array = array(); 
     while($row = mysql_fetch_assoc($result)) { 
      $menu_array[$row[self::PARENT_FIELD]][] = $row; 
     } 

     return new self($menu_array,$root_id); 
    } 

$root_id正在這裏傳遞..然後它會像魅力:)

希望它可以幫助別人在將來:)