php - 线程注释类给我数组错误

标签 php mysql oop

已修复:在我的数据库中,如果父项为空,则为 0 而非 NULL,所以我更改了 if ($comment['Parent'] === NULL)if ($comment['Parent'] < 1)现在可以了。我也改$results = $ROW$results[] = $ROW如果有人想使用这个。

谢谢

我正在尝试为我的应用程序实现一个线程注释类。但我收到错误:

Warning: Illegal string offset 'Parent' in Z:\Projects\icerik\html\test.php on line 25
Warning: Illegal string offset 'Parent' in Z:\Projects\icerik\html\test.php on line 31

数据库是 CommensID - 评论 - 父级 - ContentID

这里是原始代码code

这就是print_r($results);显示

Array (
    [CommentsID] => 131
    [ContentID] => 1171
    [UserID] => 668
    [Comment] => hic birsey yapmamislar. bu programdan sonra spor haberleri sunmus. yalanincada olmus.
    [Time] => 0000-00-00 00:00:00
    [Parent] => 130
    [WriterIP] => 82.11.180.115
    [Active] => 1
    [TotalVotes] => 0
    [VoteSum] => 0
) 

这是代码:

<?php
//database connection

class Threaded_comments  
{  

    public $parents  = array();  
    public $children = array();  

    /** 
     * @param array $comments 
     */  
    function __construct($comments)  
    {  
        foreach ($comments as $comment)  
        {  
            if ($comment['Parent'] === NULL)  
            {  
                $this->parents[$comment['CommentsID']][] = $comment;  
            }  
            else  
            {  
                $this->children[$comment['Parent']][] = $comment;  
            }  
        }  
    }  

    /** 
     * @param array $comment 
     * @param int $depth 
     */  
    private function format_comment($comment, $depth)  
    {  
        for ($depth; $depth > 0; $depth--)  
        {  
            echo "--";  
        }  

        echo $comment['Comment'];  
        echo "<br />";  
    }  

    /** 
     * @param array $comment 
     * @param int $depth 
     */  
    private function print_parent($comment, $depth = 0)  
    {  
        foreach ($comment as $c)  
        {  
            $this->format_comment($c, $depth);  

            if (isset($this->children[$c['CommentsID']]))  
            {  
                $this->print_parent($this->children[$c['CommentsID']], $depth + 1);  
            }  
        }  
    }  

    public function print_comments()  
    {  
        foreach ($this->parents as $c)  
        {  
            $this->print_parent($c);  
        }  
    }  

}

$sql = $dbc->prepare("SELECT * FROM comments WHERE ContentID = 1171");
$sql->execute();

$results = array();

while($ROW = $sql->fetch(PDO::FETCH_ASSOC))
{
    $results = $ROW;
} 

$threaded_comments = new Threaded_comments($results);  

$threaded_comments->print_comments(); 
?>

最佳答案

更改以下代码。我认为这应该适合您。

$results = array();
while($ROW = $sql->fetch(PDO::FETCH_ASSOC))
{
   $results[] = $ROW;
}

$results = $ROW;更改为$results[] = $ROW;

显示警告的原因是:

foreach ($comments as $comment)  
{
  // Here **$comment** should not be an array according to your code
  // Your code goes here  
} 

所以你可以这样做:

foreach ($comments as $key => $value)  
    {
      if($key == 'Parent') {
         $this->parents[$comments[$key]][] = $comments;
      } else {

      }
      // Your code goes here  
    }

关于php - 线程注释类给我数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18357212/

相关文章:

c# - 在 IIS 7 上的 ASP.NET MVC 4 应用程序中运行 PHP 文件?

php - 在 mysql 上的 PDO 中启用自动提交并将自动提交设置为关闭

java - 更好地设计构造函数中的初始化

c++ - c++中的成员模板函数有什么用?

javascript - 如何在 Laravel 中同时加载 js 和 css 脚本?

mysql - Over() 函数没有覆盖表中的所有行

java - 用户 'admin' @'localhost' 的访问被拒绝(使用密码 : YES)

mysql - 根据条件合并 mysql 行

python - 面向对象编程实践——概念/实体转化为对象

php - 快速访问文件