Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套注释OOP php_Php_Oop_Comments_Nested - Fatal编程技术网

嵌套注释OOP php

嵌套注释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

嗨,一整天我都在想办法。我试过这个 这和其他许多方法,但可能是由于我缺乏知识,我无法得到所需的结果。这就是我寻求帮助的原因。 我想为我的新闻网站创建嵌套评论

我在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类型的对象用作数组

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;$i
    comment
    是一个对象,而不是数组。您可以使用
    $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]);
        }