03.12 php saved
jelmer
Tags add more
1.2, ajax, ajax comments, blog tutorial extens and comments  
Note
This is some code for adding ajax comments to a post (similar to blog tutorial) using cakephp 1.2, Right now it's not working yet so help is welcome.
  1. //comments_controller.php, function add()
  2. function add()
  3.     {   
  4.         if($this->RequestHandler->isAjax())
  5.         {
  6.             if (!empty($this->data)) {
  7.                 $this->Comment->create();
  8.                 if ($this->Comment->save($this->data))
  9.                 {
  10.                     $this->Comment->recursive =-1;         
  11.                     $comments = $this->Comment->findAllByPostId($this->data['Comment']['post_id']);
  12.                     $this->set(compact('comments'));
  13.                    
  14.                     $this->viewPath = 'elements'.DS.'posts';
  15.                     $this->render('comments');
  16.                 }
  17.             }
  18.         }
  19.  
  20. //part of views/posts/view.ctp
  21.  
  22. <a name="view-comments" id="view-comments"></a>
  23. <div class="comments" id="PostComments">
  24. <h2>Comments (<? echo sizeof($post['Comment']) ?>)</h2>
  25. <? foreach ($post['Comment'] as $row=>$comment): ?>
  26. <? //if($comment['visible'] == 1){ ?>
  27. <div class="comment_<? echo $alternate->alternate($row,"1","2") ?>">
  28.     <h3><? if($comment['website']){ echo $html->link($comment['title'], $comment['website']);} else{ echo $comment['title']; } ?></h3>
  29.     <p class="subtitle">Posted on <span class="date"><? echo $date->custom_date($comment['created']); ?></span> by <span class="author"><? echo $comment['author']?></span></p>
  30.     <p><? echo $comment['body']?></p>
  31. </div>   
  32. <? /*}*/ endforeach; ?>
  33. </div>
  34.  
  35. <h2>What do you think?</h2>
  36. <!-- ajax submit form -->
  37. <?php echo $ajax->form('/comments/add', 'post', array('url' => '/comments/add', 'update' => 'PostComments', 'indicator' => 'commentSaved'));?>
  38.     <div id="commentSaved" style="display: none;"><?php echo $html->image('ajax-loader.gif'); ?></div>       
  39.     <?php
  40.     echo $form->hidden('Comment.post_id', array('value' => $post['Post']['id']));
  41.     echo $form->input('Comment.title');
  42.     echo $form->input('Comment.author');
  43.     echo $form->input('Comment.website', array('value' => 'http://'));
  44.     echo $form->input('Comment.body');
  45.     echo $form->end('Submit');
  46.     ?>
  47. <!-- end ajax submit form -->
  48. <? debug($comments) ?>       
  49.  
  50. // views/elements/posts/comments.ctp:
  51.  
  52. <h2>Comments (<? //echo sizeof($post['Comment']) ?>)</h2><!-- this is views/elements/posts/comment.ctp -->
  53. <? if(! empty($comments)){ ?>
  54. <? foreach ($comments as $row=>$comment): ?>
  55. <? //if($comment['visible'] == 1){ ?>
  56. <div class="comment_<? echo $alternate->alternate($row,"1","2") ?>">
  57.     <h3><? if($comment['website']){ echo $html->link($comment['title'], $comment['website']);} else{ echo $comment['title']; } ?></h3>
  58.     <p class="subtitle">Posted on <span class="date"><? echo $date->custom_date($comment['created']); ?></span> by <span class="author"><? echo $comment['author']?></span></p>
  59.     <p><? echo $comment['body']?></p>
  60. </div>   
  61. <? endforeach; ?>
  62. <? } ?>
  63. <? echo '<h2>Your comment has been added , Thank You!</h2>' ?>
Parsed in 0.239 seconds, using GeSHi 1.0.7.14

Modify this Paste