[英]How to build tree structure for comments?
我正在獲取博客文章的所有評論,我也通過使用以下函數的引用將它們放在樹結構中:
public function getComments($articleID) {
$sql = "SELECT * FROM comments WHERE articleid = $articleID";
$database = DatabaseFactory::getFactory()->Connect();
$stmt = $database->query($sql);
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($res!=NULL)
{
$references = array();
$tree = array();
foreach ($res as $id=> &$node) {
// Use id as key to make a references to the tree and initialize it with node reference.
$references[$node['comment_id']] = &$node;
// Add empty array to hold the children/subcategories
$node['children'] = array();
// Get your root node and add this directly to the tree
if ($node['comment_respond_to']==0) {
$tree[$node['comment_id']] = &$node;
} else {
// Add the non-root node to its parent's references
$references[$node['comment_respond_to']]['children'][$node['comment_id']] = &$node;
}
}
return $tree;
當我print_r($tree);
這是輸出;
Array
(
[1] => Array
(
[comment_id] => 1
[articleid] => 1
[user_id] => 2
[comment_comment] => First comment.
[comment_date] => 2016-03-10 20:06:43
[comment_respond_to] => 0
[children] => Array
(
[2] => Array
(
[comment_id] => 2
[articleid] => 1
[user_id] => 1
[comment_comment] => First comment, first child.
[comment_date] => 2016-03-10 20:06:43
[comment_respond_to] => 1
[children] => Array
(
)
)
[4] => Array
(
[comment_id] => 4
[articleid] => 1
[user_id] => 1
[comment_comment] => First comment, second child.
[comment_date] => 2016-03-10 20:06:43
[comment_respond_to] => 1
[children] => Array
(
)
)
)
)
[3] => Array
(
[comment_id] => 3
[articleid] => 1
[user_id] => 2
[comment_comment] => Second comment.
[comment_date] => 2016-03-10 20:06:43
[comment_respond_to] => 0
[children] => Array
(
[6] => Array
(
[comment_id] => 6
[articleid] => 1
[user_id] => 1
[comment_comment] => Second comment, first child.
[comment_date] => 2016-03-10 20:06:43
[comment_respond_to] => 3
[children] => Array
(
)
)
)
)
)
一切都很完美。
是時候回應了。
我想要的輸出:
- 第一條評論
- 第一個評論,第一個孩子。
- 第一個評論,第二個孩子。
- 第二條評論
- 第二個評論,第一個孩子。
所以,我知道我的下一步應該是使用foreach創建循環並獲取注釋。
我替換return $tree;
用;
foreach ($tree as $key) {
echo '<li>'.$key["comment_comment"], '</li>';
}
它輸出;
- 第一條評論
- 第二條評論
兒童評論缺失。 我知道我的下一步應該是在當前的foreach中創建另一個foreach以獲得孩子的評論,但我不知道該怎么做。
我感謝任何幫助。
一個簡單的解決方案是使用遞歸 ,例如
<?php
$comments = data();
foo($comments);
function foo($arr, $level=0) {
$prepend = str_repeat(' ', $level); // <- the $level thingy is not necessary; it's only in here to get a bit prettier output
echo $prepend, '<ul>', PHP_EOL;
foreach($arr as $comment) {
echo $prepend, ' <li>', $comment['comment_date'], ' ', htmlentities($comment['comment_comment']), PHP_EOL;
if ( !empty($comment['children']) ) {
foo($comment['children'], $level+1); // recurse into the next level
}
echo $prepend, ' </li>', PHP_EOL;
}
echo $prepend, '</ul>', PHP_EOL;
}
function data() {
return array (
1 =>
array (
'comment_id' => 1,
'articleid' => 1,
'user_id' => 2,
'comment_comment' => 'First comment.',
'comment_date' => '2016-03-10 20:06:43',
'comment_respond_to' => 0,
'children' =>
array (
2 =>
array (
'comment_id' => 2,
'articleid' => 1,
'user_id' => 1,
'comment_comment' => 'First comment, first child.',
'comment_date' => '2016-03-10 20:06:43',
'comment_respond_to' => 1,
'children' =>
array (
),
),
4 =>
array (
'comment_id' => 4,
'articleid' => 1,
'user_id' => 1,
'comment_comment' => 'First comment, second child.',
'comment_date' => '2016-03-10 20:06:43',
'comment_respond_to' => 1,
'children' =>
array (
14 =>
array (
'comment_id' => 14,
'articleid' => 1,
'user_id' => 99,
'comment_comment' => 'Comment to second child of First comment.',
'comment_date' => '2016-03-10 20:19:43',
'comment_respond_to' => 4,
'children' =>
array (
),
),
),
),
),
),
3 =>
array (
'comment_id' => 3,
'articleid' => 1,
'user_id' => 2,
'comment_comment' => 'Second comment.',
'comment_date' => '2016-03-10 20:06:43',
'comment_respond_to' => 0,
'children' =>
array (
6 =>
array (
'comment_id' => 6,
'articleid' => 1,
'user_id' => 1,
'comment_comment' => 'Second comment, first child.',
'comment_date' => '2016-03-10 20:06:43',
'comment_respond_to' => 3,
'children' =>
array (
),
),
),
),
);
}
版畫
<ul>
<li>2016-03-10 20:06:43 First comment.
<ul>
<li>2016-03-10 20:06:43 First comment, first child.
</li>
<li>2016-03-10 20:06:43 First comment, second child.
<ul>
<li>2016-03-10 20:19:43 Comment to second child of First comment.
</li>
</ul>
</li>
</ul>
</li>
<li>2016-03-10 20:06:43 Second comment.
<ul>
<li>2016-03-10 20:06:43 Second comment, first child.
</li>
</ul>
</li>
</ul>
您應該在foreach中使用key => value
語法。 這樣做是這樣的:
foreach ($tree as $key) {
echo '<li>'.$key["comment_comment"], '</li>';
if($key['children']) {
echo '<ul>';
foreach($key['children'] as $child) {
echo '<li>'.$child["comment_comment"], '</li>';
}
echo '</ul>';
}
}
//編輯:如果有超過1個子級別,您應該使用遞歸循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.