我有这样的表结构:
id_comment
body
date_created
date_updated
user_id
parent_id
现在这个功能:
public function comments_list($comments)
{
$comments_list = "";
foreach ($comments as $c) :
if($c['parent_id'] == 0) :
$comments_list .= '<div class="comment level-0">';
$comments_list .= $c['body'];
$comments_list .= '</div>';
endif;
endforeach;
}
$ comments 是包含所有注释的数组(2d数组)。 $ c [&#39; parent_id&#39;] == 0 是家长评论。子评论将 parent_id 设置为父评论的ID。 如何创建父/子关系的评论列表?
评论结构:
array(5) {
[0]=>
array(9) {
["id_comment"]=>
string(1) "1"
["body"]=>
string(532) "Odio lectus hac odio ultrices vel diam hac elementum amet et scelerisque porttitor nunc elementum. Sit nunc turpis! Porttitor, odio massa nisi turpis porttitor aliquam aliquam proin! Tincidunt dapibus adipiscing sagittis adipiscing odio velit eros nisi tortor porttitor ut elit enim amet egestas porta. Massa. Nec tortor aliquet sed ultrices dignissim massa! Adipiscing tortor, pulvinar ultricies, lectus adipiscing sagittis, a tortor sit magna integer. Scelerisque hac amet amet habitasse et turpis integer nisi sagittis ridiculus."
["date_created"]=>
string(19) "2014-07-01 00:00:00"
["date_updated"]=>
string(19) "2014-07-02 02:34:51"
["post_id"]=>
string(1) "4"
["user_id"]=>
string(1) "1"
["parent_id"]=>
string(1) "0"
["good_vote"]=>
string(1) "0"
["bad_vote"]=>
string(1) "0"
}
[1]=>
array(9) {
["id_comment"]=>
string(1) "2"
["body"]=>
string(532) "Odio lectus hac odio ultrices vel diam hac elementum amet et scelerisque porttitor nunc elementum. Sit nunc turpis! Porttitor, odio massa nisi turpis porttitor aliquam aliquam proin! Tincidunt dapibus adipiscing sagittis adipiscing odio velit eros nisi tortor porttitor ut elit enim amet egestas porta. Massa. Nec tortor aliquet sed ultrices dignissim massa! Adipiscing tortor, pulvinar ultricies, lectus adipiscing sagittis, a tortor sit magna integer. Scelerisque hac amet amet habitasse et turpis integer nisi sagittis ridiculus."
["date_created"]=>
string(19) "0000-00-00 00:00:00"
["date_updated"]=>
string(19) "2014-07-02 02:34:54"
["post_id"]=>
string(1) "4"
["user_id"]=>
string(1) "1"
["parent_id"]=>
string(1) "2"
["good_vote"]=>
string(1) "0"
["bad_vote"]=>
string(1) "0"
}
[2]=>
array(9) {
["id_comment"]=>
string(1) "3"
["body"]=>
string(532) "Odio lectus hac odio ultrices vel diam hac elementum amet et scelerisque porttitor nunc elementum. Sit nunc turpis! Porttitor, odio massa nisi turpis porttitor aliquam aliquam proin! Tincidunt dapibus adipiscing sagittis adipiscing odio velit eros nisi tortor porttitor ut elit enim amet egestas porta. Massa. Nec tortor aliquet sed ultrices dignissim massa! Adipiscing tortor, pulvinar ultricies, lectus adipiscing sagittis, a tortor sit magna integer. Scelerisque hac amet amet habitasse et turpis integer nisi sagittis ridiculus."
["date_created"]=>
string(19) "0000-00-00 00:00:00"
["date_updated"]=>
string(19) "2014-07-02 02:34:56"
["post_id"]=>
string(1) "4"
["user_id"]=>
string(1) "1"
["parent_id"]=>
string(1) "3"
["good_vote"]=>
string(1) "0"
["bad_vote"]=>
string(1) "0"
}
[3]=>
array(9) {
["id_comment"]=>
string(1) "4"
["body"]=>
string(532) "Odio lectus hac odio ultrices vel diam hac elementum amet et scelerisque porttitor nunc elementum. Sit nunc turpis! Porttitor, odio massa nisi turpis porttitor aliquam aliquam proin! Tincidunt dapibus adipiscing sagittis adipiscing odio velit eros nisi tortor porttitor ut elit enim amet egestas porta. Massa. Nec tortor aliquet sed ultrices dignissim massa! Adipiscing tortor, pulvinar ultricies, lectus adipiscing sagittis, a tortor sit magna integer. Scelerisque hac amet amet habitasse et turpis integer nisi sagittis ridiculus."
["date_created"]=>
string(19) "0000-00-00 00:00:00"
["date_updated"]=>
string(19) "2014-07-02 02:34:59"
["post_id"]=>
string(1) "4"
["user_id"]=>
string(1) "0"
["parent_id"]=>
string(1) "1"
["good_vote"]=>
string(1) "1"
["bad_vote"]=>
string(1) "0"
}
[4]=>
array(9) {
["id_comment"]=>
string(1) "5"
["body"]=>
string(532) "Odio lectus hac odio ultrices vel diam hac elementum amet et scelerisque porttitor nunc elementum. Sit nunc turpis! Porttitor, odio massa nisi turpis porttitor aliquam aliquam proin! Tincidunt dapibus adipiscing sagittis adipiscing odio velit eros nisi tortor porttitor ut elit enim amet egestas porta. Massa. Nec tortor aliquet sed ultrices dignissim massa! Adipiscing tortor, pulvinar ultricies, lectus adipiscing sagittis, a tortor sit magna integer. Scelerisque hac amet amet habitasse et turpis integer nisi sagittis ridiculus."
["date_created"]=>
string(19) "0000-00-00 00:00:00"
["date_updated"]=>
string(19) "2014-07-02 02:35:02"
["post_id"]=>
string(1) "4"
["user_id"]=>
string(1) "1"
["parent_id"]=>
string(1) "0"
["good_vote"]=>
string(1) "0"
["bad_vote"]=>
string(1) "0"
}
}
答案 0 :(得分:1)
您需要的基本查询是:
SELECT * FROM `comments` WHERE `parent_id` = 0 ORDER BY `date_created` DESC
假设您要查找的评论的父级具有ID 0
。
首先,只抓住&#34; root&#34; -comments:
SELECT * FROM `comments` WHERE `parent_id` IS NULL ORDER BY `date_created` DESC
假设&#34; root&#34; comment具有null值,因为它的父ID(你需要一些东西来区分它与子注释)。
修改强>
好的我想我明白你的问题是什么;)如果没有,请澄清!
你应该采取两个步骤:1)抓取根注释并迭代它们,在途中将它们插入DOM中。 2)在每次迭代中,抓住&#39;子评论&#39;并迭代它们,将它们插入DOM
如果你想要多级评论,你应该使用递归。
示例:
$query = 'SELECT * FROM `comments` WHERE `parent_id` IS NULL ORDER BY `date_created` DESC';
$comments = fetch_results_with_your_own_database_function($query);
foreach($comments as $comment) {
echo '<div class="comment">';
echo $comment->body;
echo subCommentList($comment->id);
echo '</div>';
}
function subCommentList($parent_id) {
$query = 'SELECT * FROM `comments` WHERE `parent_id` = '. $parent_id .' ORDER BY `date_created` DESC';
$comments = fetch_results_with_your_own_database_function($query);
foreach($comments as $comment) {
echo '<div class="subcomment">';
echo $comment->body;
echo subCommentList($comment->id);
echo '</div>';
}
}
请注意,该功能正在调用自身,并将当前ID作为其下一个呼叫的父节点。每次它会更深入你的列堆栈(原谅我使用条款的不当)。
警告包括查询中的ID是非常不安全的。我相信你使用一个好的数据库库和准备好的陈述等。如果你还没有使用它,请谷歌
答案 1 :(得分:0)
我认为您实际上是在同一张桌子上寻找左连接:
SELECT comments AS child, parent_id AS parent, date_created AS datecreated FROM comments children LEFT JOIN comments parents ON children.parent_id = parents.id ORDER BY parent, datecreated
这应该会为您提供父项评论列表,其中包含正在其下的子项,按创建日期排序(可能是创建它们的顺序):
| child | parent_id | datecreated |
| comment1 | | time1 | <-- root comment
| comment2 | id1 | time2 |
| comment3 | id1 | time3 |
| comment4 | id2 | time4 |
| comment5 | id3 | time5 |
然后可能写一个循环将每个这些组合成正确的数组?只是头脑风暴。这应该让你至少在正确的轨道上思考。
查看Sql Joining a Table To Itself它很好地解释了自我加入,并提供了一个很好的教程,说明如何做到这一点。