namespace Home\Controller; use Think\Controller; class IndexController extends Controller { public function index(){ $this->display(); } public function del($id){ $result=false; $User=M("user"); $result=$User->Where(‘id=‘.$id)->delete(); if($result==false){ echo json_encode(array(‘msg‘=>‘删除出错!‘)); }else{ echo json_encode(array(‘success‘=>true)); } } public function save($id){ $result=false; $User = M("User"); $data[‘firstname‘] = $_REQUEST[‘firstname‘]; $data[‘lastname‘] = $_REQUEST[‘lastname‘]; $data[‘phone‘] = $_REQUEST[‘phone‘]; $data[‘email‘] = $_REQUEST[‘email‘]; $result=$User->where(‘id=‘.$_REQUEST[‘id‘])->save($data); if($result==true){ echo json_encode(array(‘success‘=>true)); }else{ echo json_encode(array(‘msg‘=>‘更新出错!‘)); } } public function add(){ $result=false; $User=M("User"); $data[‘firstname‘] = $_REQUEST[‘firstname‘]; $data[‘lastname‘] = $_REQUEST[‘lastname‘]; $data[‘phone‘] = $_REQUEST[‘phone‘]; $data[‘email‘] = $_REQUEST[‘email‘]; $result=$User->add($data); if($result==true){ echo json_encode(array(‘success‘=>true)); }else{ echo json_encode(array(‘msg‘=>‘添加出错!‘)); } } public function read(){ $pagenum=isset($_POST[‘page‘]) ? intval($_POST[‘page‘]) : 1; $rowsnum=isset($_POST[‘rows‘]) ? intval($_POST[‘rows‘]) : 10; $User=M("User"); $total = $User->count(); //计算总数 $userlist=array(); $userlist=$User->limit(($pagenum-1)*$rowsnum.‘,‘.$rowsnum)->order(‘id asc‘)->select(); $json=‘{"total":‘.$total.‘,"rows":‘.json_encode($userlist).‘}‘;//重要,easyui的标准数据格式,数据总数和数据内容在同一个json中 echo $json; } }
时间: 2024-10-08 09:36:40