How to display comment form below each post in index view?

Hi guys,

I would like to display a comment form below each post in a multi-post index view, but can’t figure out how to structure this in a proper MVC way. My actionIndex function would be the logical place to create the new comment but this would only pass one comment to the entire dataProvider:


$dataProvider = new CActiveDataProvider('Post',array('sort'=>array('defaultOrder'=>'create_time DESC',),'pagination'=>array('pageSize'=>10,'pageVar'=>'page')));


$comment = new $comment;

$this->render('index',array(

'dataProvider'=>$dataProvider,

'comment'=>$comment,

));

Help?

Thanks,

J

Check out the Yii Blog Tutorial it has exactly that, you can copy and paste the sections you need

Thanks for your reply, but maybe I’m missing something? The tutorial seems to cover how to add a comment form to the single post view. But I would like to have a form under each post in a multi-post view. So my page would look like this:

POST #1

COMMENT #1


COMMENT #2


COMMENT #3


  COMMENT FORM

POST #2

COMMENT #1


COMMENT #2


  COMMENT FORM

POST #3

COMMENT #1


COMMENT #2


  COMMENT FORM

Trouble is I can’t seem to create the comment form without attaching it to a specific post. But when I use CActiveDataProvider to display a paginated list of posts, where would I create the comment?

Thanks,

J

as you are passing the comment to your post/view index.php

in the index.php the widget is rendering _view.php




<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); ?>



modify it like




<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

        'comments'=>$comment,

	'itemView'=>'_view',

)); ?>



in the _view create the new form of the comment model

note for multiple forms on one view u will need different form ids

this is to view the comments

create a new file _comments.php in the same folder i-e posts/view

now go to your _view.php and use this




<?php   $this->renderPartial('_comments',array(

            'comments'=>$comment,

  )); ?>



and in the _comments.php use a for each loop to get the comments values like




<?php foreach($comments as $comment):

          echo $comment['id'];

      endforeach;

 ?>




hope it works for u…

Should it be possible to post multiple comments to multiple post via a "master submit button" or partial via AJAX.

OR

Should it be only possible to create one comment to one post?

With the first one you need some kind of tabular input handling.

With the second you could e.g. create one comment form for all posts but show it only on the current post/comment via mouseover. With this solution you know to which post the comment belongs cause you know where you have inserted the comment form in the DOM (via javascript).

i have created a recent activity wall but it uses pure html and jQuery ajax i was having issues with using yii ajax and forms here is what i had done i guess u are going the same thing