2012-09-07 57 views
0

我爲Codeigniter創建了一個嵌套註釋庫,它的幾乎可以工作如何輸出不帶回聲的嵌套/螺紋註釋

我似乎無法輸出嵌套的評論而沒有迴應每個<ul><li>元素。我不希望庫直接寫任何東西,我想將它保存到一個變量並返回它,以便我可以在視圖內回顯它。

這裏是庫代碼:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Comments 
{ 
    public $parents = array(); 
    public $children = array(); 

    public function init($comments) 
    { 
     foreach ($comments as $comment) 
     { 
      if ($comment['parent_comment_id'] === NULL) 
      { 
       $this->parents[$comment['comment_id']][] = $comment; 
      } 
      else 
      { 
       $this->children[$comment['parent_comment_id']][] = $comment; 
      } 
     } 

     $this->prepare($this->parents); 

    } // End of init 

    public function thread($comments) 
    { 
     if(count($comments)) 
     { 
      echo '<ul>'; 

      foreach($comments as $c) 
      { 
       echo "<li>" . $c['text']; 
       //Rest of what ever you want to do with each row 

       if (isset($this->children[$c['comment_id']])) { 
        $this->thread($this->children[$c['comment_id']]); 
       } 

       echo "</li>"; 
      } 
      echo "</ul>"; 
     } 
    } // End of thread 

    private function prepare() 
    { 
     foreach ($this->parents as $comment) 
     { 
      $this->thread($comment); 
     } 
    } // End of prepare 

} // End of Comments class 

上面的代碼生成:

- Parent 
    - Child 
     - Child Third level 
- Second Parent 
    - Second Child 

或HTML:

<ul> 
    <li>Parent 
     <ul> 
     <li>Child 
      <ul> 
       <li>Child Third level</li> 
      </ul> 
     </li> 
     </ul> 
    </li> 
</ul> 

<ul> 
    <li>Second Parent 
     <ul> 
      <li>Second Child</li> 
     </ul> 
    </li> 
</ul> 

這是正確的HTML,但呼應出來是不可取的。

我試圖做的是:

public function thread($comments) 
    { 
     if(count($comments)) 
     { 
      $output = '<ul>'; 

      foreach($comments as $c) 
      { 
       $output .= "<li>" . $c['text']; 
       //Rest of what ever you want to do with each row 

       if (isset($this->children[$c['comment_id']])) { 
        $this->thread($this->children[$c['comment_id']]); 
       } 

       $output .= "</li>"; 
      } 
      $output .= "</ul>"; 

      echo $output; 
     } 
    } // End of thread 

預期這不工作,它生成回顯的時候以下幾點:

- Child Third level 
- Child 
- Parent 
- Second Child 
- Second Parent 

或HTML:

<ul><li>Child Third level</li></ul> 
<ul><li>Child</li></ul> 
<ul><li>Parent</li></ul> 
<ul><li>Second Child</li></ul> 
<ul><li>Second Parent</li></ul> 

這顯然不是所期望的,因爲它不會嵌套註釋。

我一直堅持這一整天,任何人都有我如何能得到正確生成列表的建議?

回答

1

試試這個:

public function thread($comments,$output='') 
{ 
    if(count($comments)) 
    { 
     $output. = '<ul>'; 

     foreach($comments as $c) 
     { 
      $output .= "<li>" . $c['text']; 
      //Rest of what ever you want to do with each row 

      if (isset($this->children[$c['comment_id']])) { 
       $output.=$this->thread($this->children[$c['comment_id']],$output); 
      } 

      $output .= "</li>"; 
     } 
     $output .= "</ul>"; 

     return $output; 
    } 
} 

我沒有測試它,但它看起來像它應該工作!

+0

這樣做了!非常感謝!我感到非常沮喪。現在我只需要弄清楚如何正確地返回它。我想要做的是將它保存到我的控制器中的一個變量,以便我可以從視圖中回顯它。因此,控制器會有'$ data ['comments'] = $ this-> comments-> init($ comments);'我可以如何獲得thread()的返回值作爲整個類的返回值?我感謝您的幫助! – Motive

+0

那麼init不會返回任何我能看到的,它大多隻是一個setter函數,所以只需調用它('$ this-> comments-> init($ comments)'),然後設置視圖變量:'$數據[ '註釋'] = $這個 - >評論 - >螺紋($意見);'。我相信會有效... –

0

問題在於,在遞歸調用完成之前,您沒有爲輸出級別回顯輸出,因爲您正在將事情累積到$output變量中。而不是容納它,只是在那一刻回聲:

public function thread($comments) 
    { 
     if(count($comments)) 
     { 
      echo'<ul>'; 

      foreach($comments as $c) 
      { 
       echo "<li>" . $c['text']; 
       //Rest of what ever you want to do with each row 

       if (isset($this->children[$c['comment_id']])) { 
        $this->thread($this->children[$c['comment_id']]); 
       } 

       echo "</li>"; 
      } 
      echo "</ul>"; 
     } 
    } // End of thread