欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Yii实现单用户博客系统文章详情页插入评论表单的方法

程序员文章站 2023-02-22 16:55:31
本文实例讲述了yii实现单用户博客系统文章详情页插入评论表单的方法。分享给大家供大家参考,具体如下: action部分:

本文实例讲述了yii实现单用户博客系统文章详情页插入评论表单的方法。分享给大家供大家参考,具体如下:

action部分:

<?php
function test($objs)
{
 $objs->var=10;
}
class one
{
 public $var=1;
}
$obj=new one();
echo $obj->var.'<p>';
test($obj);
echo $obj->var;
exit;

postcontroller.php页面:

...
/**
* displays a particular model.
* @param integer $id the id of the model to be displayed
*/
public function actionview($id)
{
  $post=$this->loadmodel($id);
  $comment=$this->newcomment($post);
  $this->render('view',array(
    'model'=>$post,
    'comment'=>$comment,
  ));
}
protected function newcomment($post)
{
  $comment=new comment();
  if(isset($_post['comment']))
  {
   $comment->attributes=$_post['comment'];
   if($post->addcomment($comment))//==============================
   {
    if($comment->status==comment::status_pending)
     yii::app()->user->setflash('commentsubmitted','thank you...');
    $this->refresh();
   }
  }
  return $comment;
}
...

models/post.php页面:

...
public function addcomment($comment)
{
  if(yii::app()->params['commentneedapproval'])
   $comment->status=comment::status_pending;
  else
   $comment->status=comment::status_approved;
  $comment->post_id=$this->id;
  return $comment->save();
}
...

post/view.php页面:

...
<div id="comments">
<h3>leave a comment</h3>
<?php if(yii::app()->user->hasflash('commentsubmitted')): ?>
 <div class="flash-success">
 <?php echo yii::app()->user->getflash('commentsubmitted'); ?>
 </div>
<?php else: ?>
 <?php $this->renderpartial('/comment/_form',array(
 'model'=>$comment,
 )); ?>
<?php endif; ?>
</div><!-- comments -->
...

希望本文所述对大家基于yii框架的php程序设计有所帮助。