2012-07-22 162 views
0

大家好,我一直在努力尋找解決方案。我試過這個 Nested array. Third level is disappearing 和這Trying to get threaded/nested comments in PHP和許多其他方式,但可能是由於我缺乏知識我無法得到所需的結果。這就是我尋求幫助的原因。 我想爲我的新聞網站創建嵌套註釋。嵌套評論OOP php

我在mysql用CommentID和PARENTID表

有一個類帖子裏我得到的所有分配的註釋

case Comments: 
    if ($this->iPostID != 0) { 
     $sSQL = "SELECT CommentID, ParentID FROM Comment WHERE PostID=" . $this->iPostID; 

     $rsComment = $this->dDatabase->query($sSQL); 

     while ($aComment = $this->dDatabase->fetch_array($rsComment)) { 
      $sComment = new comment(); 
      $sComment->load($aComment['CommentID']); 
      $this->aComments[] = $sComment; 
     } 

    } 
    return $this->aComments; 
    break; 

即數組我得到的形式$這個 - > aComments:

Array 
(
    [0] => comment Object 
     (
      [iCommentID:comment:private] => 1 
      [iDatePosted:comment:private] => 17 July 2012 
      [sContent:comment:private] => Very nice it works now 
      [iUserID:comment:private] => 1 
      [iPostID:comment:private] => 1 
      [iParentID:comment:private] => 0 
      [dDatabase:comment:private] => database Object 
       (
        [sqliConnection:database:private] => mysqli Object 
         (
          [affected_rows] => 1 
          [client_info] => 5.5.9 
          [client_version] => 50509 
          [connect_errno] => 0 
          [connect_error] => 
          [errno] => 0 
          [error] => 
          [field_count] => 6 
          [host_info] => Localhost via UNIX socket 
          [info] => 
          [insert_id] => 0 
          [server_info] => 5.5.9 
          [server_version] => 50509 
          [sqlstate] => 00000 
          [protocol_version] => 10 
          [thread_id] => 2929 
          [warning_count] => 0 
         ) 

       ) 

     ) 

... 
    [3] => comment Object 
    (
     [iCommentID:comment:private] => 4 
     [iDatePosted:comment:private] => 22 July 2012 
     [sContent:comment:private] => thies is the first reply for a comment 
     [iUserID:comment:private] => 4 
     [iPostID:comment:private] => 1 
     [iParentID:comment:private] => 1 
     [dDatabase:comment:private] => database Object 
      (
       [sqliConnection:database:private] => mysqli Object 
        (
         [affected_rows] => 1 
         [client_info] => 5.5.9 
         [client_version] => 50509 
         [connect_errno] => 0 
         [connect_error] => 
         [errno] => 0 
         [error] => 
         [field_count] => 6 
         [host_info] => Localhost via UNIX socket 
         [info] => 
         [insert_id] => 0 
         [server_info] => 5.5.9 
         [server_version] => 50509 
         [sqlstate] => 00000 
         [protocol_version] => 10 
         [thread_id] => 2929 
         [warning_count] => 0 
        ) 

      ) 

    ) 

這是我得到我想要這個數組做任何事,每次錯誤

Fatal error: Cannot use object of type comment as array in /Applications/MAMP/htdocs/News/includes/thread.php on line 15

的Thread.PHP是http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/

完全相同的副本任何人都可以幫我請。

謝謝。

+0

'comment'是一個對象,而不是一個數組。您可以使用'$ comment-> iCommentID'來獲取您的值,或者使用[ArrayObject](http://php.net/manual/en/class.arrayobject.php)允許對象作爲數組工作。 – Romain 2012-07-22 08:02:05

+1

仔細閱讀您的錯誤消息。它闡明瞭這個問題。數據結構中的每個註釋行都是一個*對象*,並且您試圖以*數組*的形式訪問它,就像您鏈接到的示例一樣。將所有訪問從'$ comment ['parent_id']'轉換爲'$ comment-> parent_id'。 (另外,'mutithreading'標籤不適合這個問題。) – DCoder 2012-07-22 08:02:21

+0

感謝您的快速回復我會嘗試您的建議。並抱歉標籤我是一個新的這個網站)) – 2012-07-22 08:13:13

回答

0

錯誤足夠詳細,thread.php假設$commment是一個數組,其中包含以下鍵parent_id,id ......在你的情況下,它是一個對象。

所以,你有兩個選擇

  • 要麼改變你的代碼,使其與thread.php(高不推薦)兼容
  • 或修改thread.php,使其處理你的對象。

要修改thread.php再次,你有兩個選擇,因爲你的Comment類屬性是private可以

  • 要麼改變訪問修飾符來public
  • 或設置一些getters

然後每一個地方在thread.php你看到這樣$comment['parent_id']使其$comment->parent_id(或$comment-getParentId如果您使用的getter)等

+0

是啊!現在一切正常)))不能相信,我花了7個小時,試圖找出我自己,並沒有先問這裏! – 2012-07-22 08:23:18

0

謝謝大家的幫助和答覆,你幫了我很多忙。 我也稍微修改我的評論系統,在這裏到底是結果

PHP:

public function load($iCommID) 
{ 
    $sSQL = "SELECT CommentID, DATE_FORMAT(DatePosted, '%d %M %Y') as DatePosted, Content, UserID, PostID, ParentID FROM Comment WHERE CommentID=" .$iCommID; 

    $aComment = $this->dDatabase->query($sSQL); 

    $rsComment = $this->dDatabase->fetch_array($aComment); 
    $this->iCommentID = $rsComment['CommentID']; 
    $this->iDatePosted = $rsComment['DatePosted']; 
    $this->sContent = $rsComment['Content']; 
    $this->iUserID = $rsComment['UserID']; 
    $this->iPostID = $rsComment['PostID']; 
    $this->iParentID = $rsComment['ParentID']; 

    $sSQL = "SELECT CommentID FROM Comment WHERE ParentID=" .$iCommID; 

      $resParent = $this->dDatabase->query($sSQL); 

      while($aReply = $this->dDatabase->fetch_array($resParent)) 
      { 

       $oReply = new comment(); 
       $oReply->load($aReply['CommentID']); 

       $this->aReply[] = $oReply; 
      } 

} 

渲染:

public static function renderSingleComment($comComment) 
{ 
    $aReplies = $comComment->Replies; 
    $sHTML = ""; 
    $sHTML .= '<li class="comment"> 
         <a id="'.$comComment->CommentID.'"></a> 
         <div class="comm-container"> 
          <div class="comm-container-header"> 
           <img src="img/avatar1.jpg" alt="avatar1" width="55" height="60" class="avatar"/> 
           <span class="commentator">Igor Revenko</span> 
           <br/> 
           <span class="date">'.$comComment->DatePosted.'</span> 
           <span><a href="#'.$comComment->ParentID.'">#</a></span> 
           <div class="clear"></div> 
          </div> 
          <div class="comm-container-entry" id="rev"> 
           <p>'.$comComment->Content.'</p> 

            <a class="comment-reply-link" id="replyLink-'.$comComment->CommentID.'" href="#'.$comComment->CommentID.'" onclick="javascript:moveForm(this); findID(\'replyLink-'.$comComment->CommentID.'\')"></a> 

          </div> 
         </div>'; 
        for($i=0;$i<count($aReplies); $i++) 
        { 
         $sHTML .= '<ul class="children">'; 
         $sHTML .= PageView::renderSingleComment($aReplies[$i]); 
         $sHTML .= '</ul>'; 
        } 
    $sHTML .= ' </li>'; 


    return $sHTML; 
} 


public static function renderComment ($pvPostID){ 


    $sHTML = ""; 

    $aComments = $pvPostID->Comments; 

    $sHTML .= '<div class="clear"></div> 
      <div id="comments"> 
       <h3>COMMENTS TO "'.$pvPostID->PostName.'"</h3> 
       <ol class="comments-list">'; 

    for($i=0; $i<count($aComments); $i++) 
    { 
     $sHTML .= PageView::renderSingleComment($aComments[$i]); 
    } 

再次感謝您的幫助。