大家好,我一直在努力寻找解决方案。我试过这个 Nested array. Third level is disappearing 这Trying to get threaded/nested comments in 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;
这就是我正在获得的数组$ this-> 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
)
)
)
这是我每次尝试使用此数组做任何事情时都会遇到的错误
致命错误:不能在第15行的/Applications/MAMP/htdocs/News/includes/thread.php中使用类型为comment的对象作为数组
Thread.PHP是http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/
的精确副本有人可以帮帮我吗。
谢谢。
答案 0 :(得分:0)
错误很详细,thread.php
假设$commment
是一个包含以下键parent_id
,id
的数组...在您的情况下,它是一个对象
所以你有两个选择
thread.php
(非常不推荐)thread.php
以使其处理您的对象。要再次修改thread.php
,您有两个选项,因为您的Comment
类属性为private
,您可以
public
。然后thread.php
中$comment['parent_id']
的每个位置都会显示$comment->parent_id
$comment-getParentId
(如果您使用了getter,则为{{1}})等等
答案 1 :(得分: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]);
}
再次感谢您的帮助。