已修复:在我的数据库中,如果 Parent 是 emty 是 0 而不是 NULL,所以我更改if ($comment['Parent'] === NULL)
为if ($comment['Parent'] < 1)
它现在可以使用。如果有人想使用它,$results = $ROW
我也会更改。$results[] = $ROW
谢谢
我正在尝试为我的应用程序实现一个线程注释类。但我收到一个错误:
警告:第 25 行 Z:\Projects\icerik\html\test.php 中的
非法字符串偏移 'Parent' 警告:第 31 行 Z:\Projects\icerik\html\test.php 中的非法字符串偏移 'Parent'
数据库是 CommensID - 评论 - 父级 - ContentID
这是原始代码代码
这是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();
?>