利用 Info 表 和 Nation 表,做数据的增删改
//显示所有数据的方法 function ShowInfo() { $model = D("Info"); $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(); $this->assign("shuju",$attr); $this->display(); } //删除数据 function ShanChu($code) { $model = D("info"); $z = $model->delete($code); if($z) { $this->success("删除成功",U("ShowInfo"),2); } else { $this->error("删除失败","ShowInfo",2); } } //添加数据 function TianJia() { $model = D("info"); if(empty($_POST)) { $this->display(); } else { $model->create(); $model->Sex = $_POST["Sex"]==1?true:false; $z = $model->add(); if($z) { $this->success("添加成功",U("TianJia"),2); } else { $this->error("添加失败",U("TianJia")); } } } //修改数据 function XiuGai($code) { $model = D("info"); $modeln = D("Nation"); if(empty($_POST)) { $attr = $model->find($code); $attrn = $modeln->select(); $this->assign("minzu",$attrn); $this->assign("shuju",$attr); $this->display(); } else { $model->create(); $model->Sex= $_POST["Sex"]==1?true:false; $z = $model->save(); if($z) { $this->success("修改成功",U("ShowInfo"),2); } else { $this->error("修改失败",U("ShowInfo"),2); } } }
ShowInfo.html
1 <body> 2 <h1>主页面</h1> 3 4 <table border="1" width="100%" cellpadding="0" cellspacing="0"> 5 <tr> 6 <td>代号</td> 7 <td>姓名</td> 8 <td>性别</td> 9 <td>民族</td> 10 <td>生日</td> 11 <td>操作</td> 12 </tr> 13 14 <foreach name="shuju" item="v"> 15 <tr> 16 <td><{$v.infocode}></td> 17 <td><{$v.infoname}></td> 18 <td><{$v["sex"]=="1"?"男":"女"}></td> 19 <td><{$v.nationname}></td> 20 <td><{$v.birthday}></td> 21 <td> 22 <a href="__CONTROLLER__/XiuGai/code/<{$v.infocode}>" >修改</a> 23 <a href="__CONTROLLER__/ShanChu/code/<{$v.infocode}>">删除</a> 24 </td> 25 </tr> 26 </foreach> 27 28 </table> 29 <br /> 30 <div><a href="__CONTROLLER__/TianJia">添加数据</a></div> 31 </body> 32 </html>
TianJia.html
<body> <h1>添加页面</h1> <form action="__ACTION__" method="post"> <div>代号:<input type="text" name="Code" /></div> <div>姓名:<input type="text" name="Name" /></div> <div>性别:<input type="text" name="Sex" /></div> <div>民族:<input type="text" name="Nation" /></div> <div>生日:<input type="text" name="Birthday" /></div> <input type="submit" value="添加" /> </form> </body> </html>
XiuGai.html
1 <body> 2 3 <h1>修改数据</h1> 4 <form action="__ACTION__/code/<{$shuju.code}>" method="post"> 5 <div>代号:<input type="hidden" name="Code" value="<{$shuju.code}>"/></div> 6 <div>姓名:<input type="text" name="Name" value="<{$shuju.name}>"/></div> 7 8 <!--<div>性别:<input type="text" name="Sex" value="<{$shuju.sex}>"/></div>--> 9 <div>性别: 10 <input type="radio" name="Sex" value="1" <{$shuju["sex"]?"checked=‘checked‘":""}> />男 11 <input type="radio" name="Sex" value="0" <{$shuju["sex"]?"":"checked=‘checked‘"}> />女 12 </div> 13 14 <!--<div>名族:<input type="text" name="Nation" value="<{$shuju.nation}>"/></div>--> 15 <div>民族: 16 <select name="Nation"> 17 <foreach name="minzu" item="v"> 18 <if condition="$v[‘code‘] == $shuju[‘nation‘]"> 19 <option value="<{$v.code}>" selected="selected" ><{$v.name}></option> 20 <else/> 21 <option value="<{$v.code}>" ><{$v.name}></option> 22 </if> 23 </foreach> 24 </select> 25 26 </div> 27 28 <div>生日:<input type="text" name="Birthday" value="<{$shuju.birthday}>"/></div> 29 <div><input type="submit" value="修改" /></div> 30 </form> 31 32 33 34 35 </body> 36 </html>
运行结果:
时间: 2024-10-04 03:22:27