一个正在学习的小菜鸟第一次写博客,请多多指教。
Controllers\TestController:
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/4/9
* Time: 15:36
*/
use Phalcon\Mvc\Controller;
class TestController extends Controller{
public function CommentAction(){
//数据库
$comment1 = new Comment1();
//获取数据
$comment1->com = $this->request->getPost("com");
//存入数据
$success = $comment1->save();
if($success){
//成功跳转到Test\suc
return $this->dispatcher->forward(
[
"controller"=>"Test",
"action"=>"suc"
]
);
}else{
$this->flash->error(
"失败"
);
}
}
//成功,显示出所有评论的数据
public function SucAction(){
//查询数据库
$comment1 = Comment1::find();
//转换格式为数组
$comment1 = $comment1->toArray();
//渲染在界面
$this->view->setVar("comment1",$comment1);
}
}
Views:
comment.phtml
<?php $this->flash->output();?>
<h2>发表感言</h2>
<?php echo $this->tag->form("../test/test/comment"); ?>
<textarea style="width:300px;height:100px;" name="com"></textarea>
<?php echo $this->tag->submitButton("提交") ?>
suc.phtml
<table width="50%" border="1">
<meta http-equiv="Content-Type" content=" charset=utf-8" />
<caption><h3>查看评论</h3></caption>
<tr>
<td>编号</td>
<td>评论内容</td>
</tr>
<?php
foreach ($comment1 as $com){
?>
<tr>
<td>
<?php echo $com['id'];?>
</td>
<td>
<?php echo $com['com'];?>
</td>
</tr>
<?php
}
?>
</table>
Models\Comment1:
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/3/12
* Time: 10:29
*/
use Phalcon\Mvc\Model;
class Comment1 extends Model{
public $id;
public $com;
}
数据库comment1表:
运行结果: