因为项目的需求,写了一个简单的 Action,其中包含了使用率较高的增删改查了,很简单,仅供自己参考吧,因为用的频率较高,所以每次写到增删改查操作的时候都要用到,所以放在博客里面,在需要的时候可以进行拷贝了,呵呵。懒人嘛,什么东西都是写了一遍就不想再写了。
- <?php
- // 本类由系统自动生成,仅供测试用途
- class GradeAction extends CommonAction {
- //列表
- public function index(){
- $model = M('grade');
- import("@.ORG.Page");
- $count = $model->count();
- $Page = new Page($count);
- $list = $model->limit($Page->firstRow. ',' . $Page->listRows)->order('id desc')->select();
- $this->list=$list;
- $this->page=$Page->show();
- $this->display();
- }
- //添加
- public function add(){
- $model = M('grade');
- if ($_POST){
- if (!I("post.title")){
- $this->error('名称不能为空');
- } //xiariboke.com
- $data['title'] = I("post.title"); //风险名称
- $data['describe'] = I("post.describe"); //风险描述
- if ($model->add($data)){
- $this->success('添加成功',U('Grade/index'));
- }else {
- $this->error('添加失败');
- }
- }else{
- $this->display();
- }
- }
- //修改
- public function edit(){
- $id = I("get.id");
- $model = M('grade');
- if ($_POST){
- if (!I("post.title")){
- $this->error('名称不能为空');
- }
- $title = I("post.title"); //风险名称
- $describe = I("post.describe"); //风险描述
- $model->where(array('id'=>$id))->save($_POST);
- $this->success('修改成功',U('Grade/index'));
- }else{
- $where['id'] = array('eq',$id);
- $this->list = $model->where($where)->find();
- $this->display();
- }
- }
- //删除
- public function del(){
- $id = I("get.id");
- $model = D('grade');
- if($model->where("id=$id")->delete()){
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }
- }
模板 index.html 文件代码如下:
- <volist name="list" id="vo">
- <tr>
- <td><input type="checkbox" name="key" value="{$vo.id}"></td>
- <td>{$vo.id}</td>
- <td>{$vo.title}</td>
- <td>{$vo.describe}</td>
- <td><a href="{:U('Grade/edit',array('id'=>$vo['id']))}">修改</a> <a href="{:U('Grade/del',array('id'=>$vo['id']))}" onclick="del({$vo['id']}); return false;">删除</a></td>
- </tr>
- </volist>
- $page}
模板 add.html 代码如下:
- <form method='post' id="form1" name="form1" action="{:U('Grade/add')}" class="form-horizontal" enctype="multipart/form-data">
- <label> 等级名称: </label>
- <input type="text" name="title" class="ipt6">
- <label>等级描述: </label>
- <input type="text" name="describe">
- <button type="submit">提交</button>
- <button type="reset">重置</button>
- </form>
模板 edit.html 代码如下:
- <form method='post' id="form1" name="form1" action="{:U('Grade/edit',array('id'=>$list['id']))}" class="form-horizontal" enctype="multipart/form-data">
- <label> 风险名称: </label>
- <input type="text" name="title" class="ipt6" value="{$list.title}">
- <label> 描述:</label> //xiariboke.com
- <input type="text" name="describe" value="{$list.describe}">
- <button type="submit">提交</button>
- <button type="reset">重置</button>
- <input type="hidden" name="id" value="{$list.id}" >
- </form>
扫一扫手机访问
