CakePHP - 打印帖子的子项,如何

发布于 2024-11-28 03:38:43 字数 1412 浏览 3 评论 0原文

下面是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿嘿嘿 2024-12-05 03:38:43

它是一个嵌入式 foreach 循环解决方案吗? 呃..是的。我不认为还有其他方法。

foreach($posts 作为 $post):
    回声 $post['Post']['id'];
    foreach ($post['Children'] as $child_post){}
  结束foreach; 

Is it an embedded foreach loop the solution? Uh.. yeah. I don't think there's another way.

foreach ($posts as $post):
    echo $post['Post']['id'];
    foreach ($post['Children'] as $child_post){}
  endforeach; 
段念尘 2024-12-05 03:38:43
$childPosts = Set::extract('/Children', $posts);
$childPosts = Set::extract('/Children', $posts);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文