整合Thinkphp数据库基本操作CURD,界面datagrid采用EasyUi的Demo
1 <?php
2 class CurdAction extends Action{
3 public function del($id){
4 $result=false;
5 $User=M("user");
6 $result=$User->Where('id='.$id)->delete();
7 if($result==false){
8 echo json_encode(array('msg'=>'删除出错!'));
9 }else{
10 echo json_encode(array('success'=>true));
11 }
12 }
13 public function save($id){
14 $result=false;
15 $User = M("User");
16 $data['firstname'] = $_REQUEST['firstname'];
17 $data['lastname'] = $_REQUEST['lastname'];
18 $data['phone'] = $_REQUEST['phone'];
19 $data['email'] = $_REQUEST['email'];
20 $result=$User->where('id='.$_REQUEST['id'])->save($data);
21 if($result==true){
22 echo json_encode(array('success'=>true));
23 }else{
24 echo json_encode(array('msg'=>'更新出错!'));
25 }
26 }
27
28 public function add(){
29 $result=false;
30 $User=M("User");
31 $data['firstname'] = $_REQUEST['firstname'];
32 $data['lastname'] = $_REQUEST['lastname'];
33 $data['phone'] = $_REQUEST['phone'];
34 $data['email'] = $_REQUEST['email'];
35 $result=$User->add($data);
36 if($result==true){
37 echo json_encode(array('success'=>true));
38 }else{
39 echo json_encode(array('msg'=>'添加出错!'));
40 }
41 }
42
43 public function read(){
44 $pagenum=isset($_POST['page']) ? intval($_POST['page']) : 1;
45 $rowsnum=isset($_POST['rows']) ? intval($_POST['rows']) : 10;
46 $User=M("User");
47 $total = $User->count(); //计算总数
48 $userlist=array();
49 $userlist=$User->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->order('id asc')->select();
50 $json='{"total":'.$total.',"rows":'.json_encode($userlist).'}';//重要,easyui的标准数据格式,数据总数和数据内容在同一个json中
51 echo $json;
52 }
53 }
54 ?>
2 class CurdAction extends Action{
3 public function del($id){
4 $result=false;
5 $User=M("user");
6 $result=$User->Where('id='.$id)->delete();
7 if($result==false){
8 echo json_encode(array('msg'=>'删除出错!'));
9 }else{
10 echo json_encode(array('success'=>true));
11 }
12 }
13 public function save($id){
14 $result=false;
15 $User = M("User");
16 $data['firstname'] = $_REQUEST['firstname'];
17 $data['lastname'] = $_REQUEST['lastname'];
18 $data['phone'] = $_REQUEST['phone'];
19 $data['email'] = $_REQUEST['email'];
20 $result=$User->where('id='.$_REQUEST['id'])->save($data);
21 if($result==true){
22 echo json_encode(array('success'=>true));
23 }else{
24 echo json_encode(array('msg'=>'更新出错!'));
25 }
26 }
27
28 public function add(){
29 $result=false;
30 $User=M("User");
31 $data['firstname'] = $_REQUEST['firstname'];
32 $data['lastname'] = $_REQUEST['lastname'];
33 $data['phone'] = $_REQUEST['phone'];
34 $data['email'] = $_REQUEST['email'];
35 $result=$User->add($data);
36 if($result==true){
37 echo json_encode(array('success'=>true));
38 }else{
39 echo json_encode(array('msg'=>'添加出错!'));
40 }
41 }
42
43 public function read(){
44 $pagenum=isset($_POST['page']) ? intval($_POST['page']) : 1;
45 $rowsnum=isset($_POST['rows']) ? intval($_POST['rows']) : 10;
46 $User=M("User");
47 $total = $User->count(); //计算总数
48 $userlist=array();
49 $userlist=$User->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->order('id asc')->select();
50 $json='{"total":'.$total.',"rows":'.json_encode($userlist).'}';//重要,easyui的标准数据格式,数据总数和数据内容在同一个json中
51 echo $json;
52 }
53 }
54 ?>
以上是PHP源代码CurdAction.class.php,以下是datagrid界面