谁能帮我处理 php 线程评论吗?
我找到了一个预先编写的类脚本来创建线程注释,但在尝试实现它之后,没有任何打印。数组 si 保存数据,我已经确认了这一点,但是,调用该函数时不会打印任何内容,所以我想知道是否有人可以提供帮助。
该脚本可以在这里找到:
它也如下:
class Threaded_comments
{
public $parents = array();
public $children = array();
/**
* @param array $comments
*/
function __construct($comments)
{
foreach ($comments as $comment)
{
if ($comment['parent_id'] === NULL)
{
$this->parents[$comment['id']][] = $comment;
}
else
{
$this->children[$comment['parent_id']][] = $comment;
}
}
}
/**
* @param array $comment
* @param int $depth
*/
private function format_comment($comment, $depth)
{
for ($depth; $depth > 0; $depth--)
{
echo "\t";
}
echo $comment['text'];
echo "\n";
}
/**
* @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['id']]))
{
$this->print_parent($this->children[$c['id']], $depth + 1);
}
}
}
public function print_comments()
{
foreach ($this->parents as $c)
{
$this->print_parent($c);
}
}
}
我在germannrumm的帮助下使用的代码如下:
$q = $DBH->prepare("SELECT id, parent_id, comment FROM comments WHERE page = ?");
$q->bind_param("i", $page);
$q->execute();
$q->bind_result($id, $parent_id, $text);
$all_results = array();
while ($q->fetch()) {
$all_results[] = array(
'id' => $id,
'parent_id' => $parent_id,
'text' => $text);
}
$q->close();
非常感谢任何帮助!谢谢!
$tc = new Threaded_comments($all_results);
$tc->print_comments();
I found a pre-written script of a class to create threaded comments, but after trying to implement it, nothing prints. The array si holding data, and I have confirmed that, but, nothing will print when the function is called, so I was wondering if someone could please help.
the script can be found here:
It is also below as follows:
class Threaded_comments
{
public $parents = array();
public $children = array();
/**
* @param array $comments
*/
function __construct($comments)
{
foreach ($comments as $comment)
{
if ($comment['parent_id'] === NULL)
{
$this->parents[$comment['id']][] = $comment;
}
else
{
$this->children[$comment['parent_id']][] = $comment;
}
}
}
/**
* @param array $comment
* @param int $depth
*/
private function format_comment($comment, $depth)
{
for ($depth; $depth > 0; $depth--)
{
echo "\t";
}
echo $comment['text'];
echo "\n";
}
/**
* @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['id']]))
{
$this->print_parent($this->children[$c['id']], $depth + 1);
}
}
}
public function print_comments()
{
foreach ($this->parents as $c)
{
$this->print_parent($c);
}
}
}
the code i have used with help from germannrumm is as follows:
$q = $DBH->prepare("SELECT id, parent_id, comment FROM comments WHERE page = ?");
$q->bind_param("i", $page);
$q->execute();
$q->bind_result($id, $parent_id, $text);
$all_results = array();
while ($q->fetch()) {
$all_results[] = array(
'id' => $id,
'parent_id' => $parent_id,
'text' => $text);
}
$q->close();
any help is greatly appreciated! thanks!
$tc = new Threaded_comments($all_results);
$tc->print_comments();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该类工作正常,但我们无法为您测试您的数据库代码。确保您确实将数组传递给类。在开始课程之前执行 var_dump($all_results)。
The class is working properly, but we can't test your database code for you. Make sure that you are indeed passing an array to the class. Do a var_dump($all_results) right before you initiate the class.