foreach ($posts as $post) //main foreach for displaying posts
{
///somewhere where you want comments do this
foreach ($post['Comment'] as $comment)
{
pr($comment);
}
}
--
Lep pozdrav, Tilen Majerle
http://majerle.eu
2011/3/31 Elguapaul <[email protected]>
> I have 2 tables, posts and comments. I have setup posts hasMany
> comments and comments belongsTo posts model relations. I am using a
> foreign key in the comments table pointing back to the post id.
>
> I want to display the Post and the all related comments. At the
> moment I can display the post and a specified comment by using the
> View code below. How do I display all the comments for a post?
>
>
> View Code:
> <?php
> $i = 0;
> foreach ($posts as $post):
> $class = null;
> if ($i++ % 2 == 0) {
> $class = ' altrow';
> }
> ?>
> <div class="homepage<?php echo $class;?>">
> <p><h3><?php echo $post['Post']['title']; ?></h3></p>
> <p><?php echo $post['Post']['body']; ?></p><br />
>
> <!-- Comments below -->
> <p><?php echo $post['Comment']['0']['body'];?></p>
> </div>
> <?php endforeach; ?>
>
>
> This is the pr() output:
>
> Array
> (
> [0] => Array
> (
> [Post] => Array
> (
> [id] => 2
> [title] => A title once again
> [body] => And the post body follows.
> [created] => 2011-03-17 09:15:40
> [modified] =>
> )
>
> [Comment] => Array
> (
> [0] => Array
> (
> [id] => 2
> [post_id] => 2
> [title] => COMMENTSA title once again
> [body] => AndCOMMENTS the post body
> follows.
> [created] => 2011-03-22 16:31:03
> [modified] =>
> )
>
> [1] => Array
> (
> [id] => 4
> [post_id] => 2
> [title] =>
> [body] => This is another comment for the
> first post
> [created] => 2011-03-22 17:28:34
> [modified] => 2011-03-22 17:28:34
> )
>
> )
>
> )
>
>
> I am new to Cakephp so thanks in advance.
>
> --
> Our newest site for the community: CakePHP Video Tutorials
> http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help
> others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> [email protected] For more options, visit this group
> at http://groups.google.com/group/cake-php
>
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php