1

Hi the whole day I've been trying to find a solution. I have tried this Nested array. Third level is disappearing and this Trying to get threaded/nested comments in PHP and many other ways but may be due to my lack of knowledge I could not get the needed result. And that's why I'm asking for help. I want to create nesting comments for my News Website.

I have a table in mySQL with CommentID and ParentID

Have a Class Post where I'm getting all the assigned comments

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;

That is the array I'm getting form $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
                    )

            )

    )

And this is the error I'm getting every time I'm trying to do anything with this array

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

the Thread.PHP is an exact copy of http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/

Could anyone help me please.

Thank you.

Community
  • 1
  • 1
Revenko Igor
  • 59
  • 1
  • 8
  • `comment` is an object, not an array. Either you use `$comment->iCommentID` to get your values, or use [ArrayObject] (http://php.net/manual/en/class.arrayobject.php) to allow objects to work as arrays. – Romain Jul 22 '12 at 08:02
  • 1
    Read your error message carefully. It spells out the problem. Each comment row in your data structure is an *object* and you are attempting to access it as an *array*, like the example you linked to does. Convert all accesses from `$comment['parent_id']` to `$comment->parent_id`. (Also, the `mutithreading` tag is not appropriate for this question.) – DCoder Jul 22 '12 at 08:02
  • Thank you for quick reply I'll try your suggestion. And sorry for tags I'm a new to this site)) – Revenko Igor Jul 22 '12 at 08:13
  • Thanks DCoder everything works now, the only problem is nesting this one [http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/] didn't work for me probably will be trying other ways. – Revenko Igor Jul 22 '12 at 08:25

2 Answers2

0

The error is verbose enough, thread.php is assuming that $commment is an array that contains the following keys parent_id, id... While in your case it's an object.

So you have two options

  • Either change your code to make it compatible with thread.php (Highly unrecommended)
  • Or modify thread.php to make it deal with your objects.

To modify thread.php again you have two options, since your Comment class attributes are private you can

  • Either change the access modifiers to public.
  • Or setup up some getters

Then every where in thread.php you see something like $comment['parent_id'] make it $comment->parent_id (or $comment-getParentId if you used getters) and so on

Adi
  • 5,089
  • 6
  • 33
  • 47
  • Yeah!! everything works now ))) Can't believe that I've spend more that 7 hours trying to figure it out by my self and didn't ask here first!!! – Revenko Igor Jul 22 '12 at 08:23
0

Thanks everyone for your help and replies, you helped me lots. I did slightly modify my comment system and at the end here is the result

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;
            }

}

Render:

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]);
    }

Thank you for help again.

Revenko Igor
  • 59
  • 1
  • 8