Thinkphp 完整的增删改查 Action

 因为项目的需求,写了一个简单的 Action,其中包含了使用率较高的增删改查了,很简单,仅供自己参考吧,因为用的频率较高,所以每次写到增删改查操作的时候都要用到,所以放在博客里面,在需要的时候可以进行拷贝了,呵呵。懒人嘛,什么东西都是写了一遍就不想再写了。

 
  1. <?php  
  2. // 本类由系统自动生成,仅供测试用途  
  3. class GradeAction extends CommonAction {  
  4.     //列表  
  5.     public function index(){  
  6.           
  7.         $model = M('grade');  
  8.    
  9.         import("@.ORG.Page");   
  10.         $count  = $model->count();     
  11.         $Page   = new Page($count);  
  12.   
  13.         $list   = $model->limit($Page->firstRow. ',' . $Page->listRows)->order('id desc')->select();  
  14.         $this->list=$list;  
  15.          
  16.         $this->page=$Page->show();  
  17.         $this->display();  
  18.      }  
  19.     //添加  
  20.     public function add(){    
  21.           
  22.         $model = M('grade');  
  23.         if ($_POST){  
  24.               
  25.             if (!I("post.title")){  
  26.                 $this->error('名称不能为空');  
  27.             }  //xiariboke.com  
  28.               
  29.             $data['title'] = I("post.title");  //风险名称  
  30.             $data['describe'] = I("post.describe");  //风险描述  
  31.               
  32.              if ($model->add($data)){  
  33.                     $this->success('添加成功',U('Grade/index'));  
  34.                 }else {  
  35.                     $this->error('添加失败');  
  36.                 }  
  37.         }else{  
  38.             $this->display();  
  39.         }  
  40.     }  
  41.     //修改  
  42.     public function edit(){  
  43.       
  44.         $id     = I("get.id");  
  45.         $model = M('grade');  
  46.   
  47.         if ($_POST){  
  48.             if (!I("post.title")){  
  49.                 $this->error('名称不能为空');  
  50.             }  
  51.               
  52.             $title = I("post.title");  //风险名称  
  53.             $describe = I("post.describe");  //风险描述  
  54.               
  55.             $model->where(array('id'=>$id))->save($_POST);  
  56.             $this->success('修改成功',U('Grade/index'));  
  57.               
  58.         }else{  
  59.             $where['id'] = array('eq',$id);  
  60.             $this->list = $model->where($where)->find();  
  61.             $this->display();  
  62.         }  
  63.     }  
  64.       
  65.     //删除  
  66.     public function del(){  
  67.         $id  = I("get.id");  
  68.         $model = D('grade');  
  69.         if($model->where("id=$id")->delete()){  
  70.             $this->success('删除成功');  
  71.         }else{  
  72.             $this->error('删除失败');  
  73.         }  
  74.     }  
  75. }  

模板 index.html 文件代码如下:

 
  1. <volist name="list" id="vo">  
  2.                                        <tr>  
  3.                                            <td><input type="checkbox" name="key" value="{$vo.id}"></td>  
  4.                                            <td>{$vo.id}</td>  
  5.                                            <td>{$vo.title}</td>  
  6.                                            <td>{$vo.describe}</td>  
  7.                                            <td><a href="{:U('Grade/edit',array('id'=>$vo['id']))}">修改</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="{:U('Grade/del',array('id'=>$vo['id']))}" onclick="del({$vo['id']}); return false;">删除</a></td>  
  8.                                        </tr>  
  9.                                    </volist>  
  10. $page}  

模板 add.html 代码如下:

 
  1. <form method='post' id="form1" name="form1" action="{:U('Grade/add')}"    class="form-horizontal"  enctype="multipart/form-data">  
  2.      <label> 等级名称: </label>  
  3.          <input type="text" name="title" class="ipt6">  
  4.      <label>等级描述: </label>  
  5.          <input type="text" name="describe">  
  6.    <button type="submit">提交</button>  
  7.   <button type="reset">重置</button>  
  8. </form>  

模板 edit.html 代码如下:

 
  1. <form method='post' id="form1" name="form1" action="{:U('Grade/edit',array('id'=>$list['id']))}"  class="form-horizontal"  enctype="multipart/form-data">  
  2.         <label> 风险名称: </label>      
  3.              <input type="text" name="title" class="ipt6" value="{$list.title}">  
  4.         <label> 描述:</label> //xiariboke.com  
  5.               <input type="text" name="describe" value="{$list.describe}">  
  6.       <button type="submit">提交</button>  
  7.        <button type="reset">重置</button>  
  8.               <input type="hidden" name="id" value="{$list.id}" >  
  9. </form>  

扫一扫手机访问