6月17日 TP框架增删改查

用自动收集表单的形式进行增删改查

MainController.class.php:

<?php
namespace Home\Controller;
use Think\Controller;
class MainController extends Controller
{
        //显示
        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("shuzu",$attr);
        $this->display();
    }
    //删除
    function ShanChu($code)
    {
        $model = D("Info");
        $z = $model->delete($code);
        if($z)
        {
            $this->success("删除成功",U("ShowInfo"));
        }
        else
        {
            $this->error("删除失败",U("ShowInfo"));
        }
    }
    //添加
    function TianJia()
    {
        if(empty($_POST))
        {
            $model = D("Nation");
            $attr = $model->select();
            $this->assign("shuzu",$attr);
            $this->display();
        }
        else
        {
            $model = D("Info");
            $model->create();
            $model->Sex = $_POST["Sex"]=="1"?true:false;
            $z = $model->add();
            if($z)
            {
                $this->success("添加成功",U("ShowInfo"));
            }
            else
            {
                $this->error("添加失败");
            }
        }
    }
    //修改
    function XiuGai($code)
    {
        $model = D("Info");
        $modeln = D("Nation");
            
        if(empty($_POST))
        {            
            $attr = $model->find($code);
            $attrn = $modeln->select();
            
            $this->assign("shuju",$attr);
            $this->assign("nation",$attrn);
            
            $this->display();                        
        }
        else
        {
            $model = D("Info");
            $model->create();
            $model->Sex = $_POST["Sex"]=1?true:false;
            $z = $model->save();
            if($z)
            {
                $this->success("修改成功",U("ShowInfo"));
            }
            else
            {
                $this->error("修改失败");
            }
        }
    }

}

 

ShowInfo.html:

<body><br />
<h1>主页面</h1>
<table border="1" cellspacing="0" cellpadding="0" width="100%">
<tr>
    <td>代号</td>
    <td>姓名</td>
    <td>性别</td>
    <td>民族</td>
    <td>生日</td>
    <td>操作</td>
</tr>

<foreach name="shuzu" item="v">
<tr>
<td><{$v.infocode}></td>
<td><{$v.infoname}></td>
<td><{$v["sex"]?"男":"女"}></td>
<td><{$v.nationname}></td>
<td><{$v.birthday}></td>
<td>
<a href="__CONTROLLER__/XiuGai/code/<{$v.infocode}>">修改</a>
<a href="__CONTROLLER__/ShanChu/code/<{$v.infocode}>">删除</a>
</td>
</tr>
</foreach>

</table>
<br />
<div><a href="__CONTROLLER__/TianJia">添加数据</a></div>
</body>

 

TianJia.html:

<body>
<form action="__ACTION__" method="post">
<div>代号:<input type="text" name="Code" /></div>
<div>姓名:<input type="text" name="Name" /></div>
<div>
性别:<input type="radio" name="Sex" value="1"/><input type="radio" name="Sex" value="0"/></div>
<div>民族:
<select name="Nation">
<foreach name="shuzu" item="v">
<option value="<{$v.code}>"><{$v.name}></option>
</foreach>
</select>
</div>
<div>生日:<input type="text" name="Birthday" /></div>
<input type="submit" value="添加" />
</form>
</body>

XiuGai.html:

<body>
<form action="__ACTION__/code/<{$shuju.code}>" method="post">
<input type="hidden" name="Code" value="<{$shuju.code}>" />
<div>姓名:<input type="text" name="Name" value="<{$shuju.name}>"></div>
<div>性别 :
    <input type="radio" value="1" name="Sex" <{$shuju["sex"]?"checked='checked'":"" }>/>男
    <input type="radio" value="0" name="Sex" <{$shuju["sex"]?"":"checked='checked'" }>/>女
</div>
<div>民族:
<select>
<foreach name="nation" item="v">
    <if condition="$shuju['nation']==$v['code']">
    <option selected="selected" value="<{$v.code}>"><{$v.name}></option>
    <else/>
    <option value="<{$v.code}>"><{$v.name}></option>
    </if>
</foreach>
</select>
</div>
<div>生日:<input type="text" name="Birthday" value="<{$shuju.birthday}>" /></div>
<div><input type="submit" value="修改" /><div>
</form>
</body>

 

posted @ 2016-06-21 08:59  D董小姐  阅读(2169)  评论(0编辑  收藏  举报