数据显示页面:MainController.class.php中的方法(增删改查全包括--function ShowInfo,ShanChu,TianJia,XiuGai)
![]()
1 <?php
2 namespace Home\Controller;
3 use Think\Controller;
4 class MainController extends Controller
5 {
6
7 //例题:数据的增删改
8 //显示所有数据:
9 function ShowInfo()
10 {
11 $model=D("Info");
12 $attr=$model->field("info.code as infocode,info.name as infoname,info.sex,nation.name as nationname,info.birthday")->join("nation on info.nation=nation.code")->select();
13 $this->assign("shuju",$attr);
14 $this->display();
15
16 }
17 //删除数据:
18 function ShanChu($code)
19 {
20 $model=D("Info");
21 $r=$model->delete($code);
22 if($r)
23 {
24 $this->success("删除成功",U("ShowInfo"));
25 }
26 else
27 {
28 $this->error("删除失败");
29 }
30
31 }
32 //添加数据:
33 function TianJia()
34 {
35 if(empty($_POST))
36 {
37 $model=D("Nation");
38 $attr=$model->select();
39 $this->assign("shuju",$attr);
40 $this->display();
41 }
42 else
43 {
44 $model=D("Info");
45 $model->create();//自动收集表单数据入库
46 $model->Sex=$_POST["Sex"]=="男"?true:false;
47 $r=$model->add();
48 if($r)
49 {
50 $this->success("添加成功!","Tianjia",3);
51 }
52 else
53 {
54 $this->error("添加失败!","Tianjia",3);
55 }
56 }
57 }
58 //修改数据:
59 function XiuGai($code)
60 {
61 $model=D("info");
62 $modeln=D("nation");
63 if(empty($_POST))
64 {
65 $attr=$model->find($code);
66 $attrn=$modeln->select();
67
68 $this->assign("shuju",$attr);
69 $this->assign("shujun",$attrn);
70 $this->display();
71 }
72 else
73 {
74
75 $model->create();
76 $model->Sex=$_POST["Sex"]=="男"?true:false;
77 $r=$model->save();
78 if($r)
79 {
80 $this->success("修改成功",U("Showinfo"));
81 }
82 else
83 {
84 $this->error("修改失败!");
85 }
86
87 }
88 }
89 }
90
View Code