Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.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/8/mysql/59.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
如何在php中整理注释上的注释_Php_Mysql_Sql - Fatal编程技术网

如何在php中整理注释上的注释

如何在php中整理注释上的注释,php,mysql,sql,Php,Mysql,Sql,我想为我的网站访问者提供对现有评论发表评论的便利,因为我们可以在stackoverflow中对答案发表评论 这里我创建了两个表 threads thread_id (auto increment), title VARCHAR(MAX), body VARCHAR(MAX), date datetime, user INT comments comment_id INT (auto increment), parent_id INT,

我想为我的网站访问者提供对现有评论发表评论的便利,因为我们可以在stackoverflow中对答案发表评论

这里我创建了两个表

threads
    thread_id (auto increment),
    title VARCHAR(MAX),
    body VARCHAR(MAX),
    date datetime,
    user INT

comments
    comment_id INT (auto increment),
    parent_id INT,
    thread_id INT,
    title VARCHAR(MAX),
    body VARCHAR(MAX),
    date datetime,
    user INT
当用户在一个线程上发布评论时,它将保存在评论表中,如下所示

comment_id = 1211
parent_id = NULL
thread_id = 122
title = "This is the title of the comment"
body = "This is the Body of the comment"
date = 2013-04-04 13:05:44
user = "xyzuser"
comment_id = 1212
parent_id = 1211
thread_id = 122
title = "This is sample title of comment on comment"
body = "This is the Body of comment on comment";
date = 2013-04-04 15:05:44
user = "abcuser"
假设一个用户在上面提到的评论上发表评论,该评论将保存在评论表中,如下所示

comment_id = 1211
parent_id = NULL
thread_id = 122
title = "This is the title of the comment"
body = "This is the Body of the comment"
date = 2013-04-04 13:05:44
user = "xyzuser"
comment_id = 1212
parent_id = 1211
thread_id = 122
title = "This is sample title of comment on comment"
body = "This is the Body of comment on comment";
date = 2013-04-04 15:05:44
user = "abcuser"
我使用以下查询从threads表中获取线程

SELECT * FROM threads WHERE thread_id = 122
到目前为止,我从下面的评论表中获取线程下的评论

SELECT * FROM comments WHERE thread_id = 122
但是现在,我还想显示每个评论下的评论, 我试过跟随

SELECT * FROM comments WHERE thread_id = 122 GROUP BY parent_id
但是使用这个查询,在parent_id中具有NULL值的行会发生什么情况,以及如何对每个注释下的注释进行排序

谁有什么解决办法??我应该使用哪个查询以及如何对每个注释下的注释进行排序???

您使用的查询从注释中选择*,其中thread_id=122也将获得注释下的注释。你是不是想把它们按特定的顺序排列?然后尝试按父项id添加顺序。或者指定您希望它们的顺序

SELECT c.* FROM comments c
LEFT JOIN
  comments s
ON 
  s.parent_id = c.comment_id
WHERE c.thread_id = 122
GROUP BY c.comment_id
ORDER BY c.date ASC

也许您甚至可以绘制一个小记录集,向我们展示您希望查询结果的样子。因为我不太明白您希望它们是什么样子。

我不知道您是如何得到注释sql结果的,不管是数组还是对象,比如if数组-

$comments = array(
    array('comment_id' => '1211',
        'parent_id'  => NULL,
        'thread_id'  => '122',
        'title'      => 'This is the title of the comment',
        'body'       => 'This is the Body of the comment',
        'date'       => '2013-04-04 13:05:44',
        'user'       => 'xyzuser'),
    array('comment_id' => '1212',
        'parent_id'  => '1211',
        'thread_id'  => '122',
        'title'      => 'This is the title of the comment',
        'body'       => 'This is the Body of the comment',
        'date'       => '2013-04-04 15:05:44',
        'user'       => 'abcuser'),
);
现在对数组进行排序-

$childs = array();
foreach ($comments as &$comment) {
    $childs[$comment['parent_id']][] = &$comment;
}
unset($comment);

foreach ($comments as &$comment) {
    if (isset($childs[$comment['comment_id']])) {
        $comment['childs'] = $childs[$comment['comment_id']];
    }
}
unset($comments);

$tree = $childs[NULL];
echo "<pre>";
print_r($tree);

您也可以对object应用相同的逻辑,以按排序顺序显示结果。

嗨,Rashid,您应该看一下从《Sql反模式》一书中摘录的这一章。它确实提供了对这个常见问题的深刻见解。Cheers@jribeiro这一章真的很有用。非常感谢分享。这确实是一本非常方便的书。我的快乐就这么来了?!你是说点菜吗?