CakePHP - 打印帖子的子项,如何
下面是 posts_controller 的调试输出。我目前使用语法:
<?php foreach ($posts as $post): ?> ... <?php endforeach; ?>
打印视图中的帖子,但这只会产生最外面的帖子(即 - 父帖子且不打印子帖子)。
问题:
如何在每个父帖子的正下方打印所有子帖子? 解决方案是嵌入式 foreach 循环吗?
调试:
Array
(
[0] => Array
(
[Post] => Array
(
[active] => 1
[id] => 1
[parent_id] => 0
[created] => 2011-08-06 03:54:07
[modified] => 2011-08-06 03:54:07
[text] => a
)
[Children] => Array
(
[0] => Array
(
[active] => 1
[id] => 3
[parent_id] => 1
[created] => 2011-08-08 01:54:24
[modified] => 2011-08-08 01:54:24
[text] => c
)
[1] => Array
(
[active] => 1
[id] => 2
[parent_id] => 1
[created] => 2011-08-06 03:54:37
[modified] => 2011-08-06 03:54:37
[text] => b
)
)
)
)
Below is the debug output from a posts_controller. I currently use the syntax:
<?php foreach ($posts as $post): ?> ... <?php endforeach; ?>
to print the posts in the view, however this only yields the outer most posts (ie - the parent posts and no children posts are printed).
QUESTION:
How do I also print all the children posts, directly under each parent post?
Is the solution an embedded foreach loop?
DEBUG:
Array
(
[0] => Array
(
[Post] => Array
(
[active] => 1
[id] => 1
[parent_id] => 0
[created] => 2011-08-06 03:54:07
[modified] => 2011-08-06 03:54:07
[text] => a
)
[Children] => Array
(
[0] => Array
(
[active] => 1
[id] => 3
[parent_id] => 1
[created] => 2011-08-08 01:54:24
[modified] => 2011-08-08 01:54:24
[text] => c
)
[1] => Array
(
[active] => 1
[id] => 2
[parent_id] => 1
[created] => 2011-08-06 03:54:37
[modified] => 2011-08-06 03:54:37
[text] => b
)
)
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是一个嵌入式 foreach 循环解决方案吗?
呃..是的。我不认为还有其他方法。Is it an embedded foreach loop the solution?
Uh.. yeah. I don't think there's another way.