嵌套注释OOP php
嗨,一整天我都在想办法。我试过这个 这和其他许多方法,但可能是由于我缺乏知识,我无法得到所需的结果。这就是我寻求帮助的原因。 我想为我的新闻网站创建嵌套评论 我在mySQL中有一个带有CommentID和ParentID的表 有一个班级帖子,在那里我可以得到所有指定的评论嵌套注释OOP php,php,oop,comments,nested,Php,Oop,Comments,Nested,嗨,一整天我都在想办法。我试过这个 这和其他许多方法,但可能是由于我缺乏知识,我无法得到所需的结果。这就是我寻求帮助的原因。 我想为我的新闻网站创建嵌套评论 我在mySQL中有一个带有CommentID和ParentID的表 有一个班级帖子,在那里我可以得到所有指定的评论 case Comments: if ($this->iPostID != 0) { $sSQL = "SELECT CommentID, ParentID FROM Comment WHERE P
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类型的对象用作数组
PHP是
谁能帮帮我吗
谢谢。错误足够详细,
thread.php
假设$commment
是一个数组,包含以下键父id
,id
。。。而在你的情况下,它是一个对象
所以你有两个选择
- 或者更改代码,使其与
兼容(高度不推荐)thread.php
- 或者修改
,使其能够处理您的对象thread.php
thread.php
,您有两个选项,因为您的Comment
类属性是private
,您可以
- 将访问修饰符更改为
public
- 或者设置一些
然后在
thread.php
中的每个地方,你都会看到类似于$comment['parent\u id']
的东西,让它成为$comment->parent\u id
(或者$comment getParentId
,如果你使用getters)等等谢谢大家的帮助和回复,你帮了我很多。
我确实稍微修改了我的评论系统,最后是结果
PHP:
呈现:
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]);
}
公共静态函数renderSingleComment($comComment)
{
$aReplies=$comComment->回复;
$sHTML=“”;
$sHTML.='
“.$comComment->Content”
';
对于($i=0;$iComments;
$sHTML.='
对“'.$pvPostID->PostName.”的注释
';
for($i=0;$icomment
是一个对象,而不是数组。您可以使用$comment->iCommentID
获取值,或者使用[ArrayObject]()允许对象作为数组工作。请仔细阅读错误消息。它说明了问题所在。数据结构中的每个注释行都是一个对象,您正试图将其作为数组访问,就像链接到的示例一样。将所有访问从$comment['parent\u id']
转换为$comment->parent\u id
。(另外,multithreading
标签不适合这个问题。)谢谢你的快速回复,我会尝试你的建议。对于标签我是这个网站的新成员,我很抱歉。)谢谢你。现在一切都好了,唯一的问题是嵌套这个(对我不好,可能会尝试其他方式。是的!!一切都好了)))真不敢相信,我花了7个多小时的时间试图自己解决这个问题,却没有在这里先问一下!!!
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]);
}