TP5

TP5 是一个快捷,简单的基于MVC和面向对象的轻量级PHP开发框架,为WEB应用和API开发提供了强有力的支持

 

<?php
namespace app\index\controller;
use app\common\controller\AdminBase;
use think\Db;
use think\exception\PDOException;

class Student extends AdminBase
{
    //显示所有学生列表
    public function all()
    {
        //select方法,返回二维数组
        $data = Db::name('student')->alias("a")
            ->join('__BANJI__ i','a.classid = i.classid')
            ->field('a.no,a.name,a.sex,a.age,i.classid,i.classname,a.photo')
            ->order('no')
            ->select();
        $this->assign('student', $data);
        return $this->fetch();
    }
    //显示添加学生的页面
    public function add()
    {
        $data = Db::name('banji')->select();
        $this->assign('banji', $data);
        return $this->fetch();
    }
    //执行添加操作
    public function do_add()
    {
        $data = input('post.');
        try
        {
            $ret = Db::name('student')->insert($data);
            $this->success('添加成功', 'all');
        }
        catch (PDOException $ex)
        {
            $this->error('添加失败,' . $ex->getMessage());
        }
    }
    //显示编辑学生的页面
    public function edit($no)

    {
        $data = Db::name('student')->where('no', $no)->find();
        $this->assign('student', $data);
        return $this->fetch();
    }
    //执行编辑操作
    public function do_edit()
    {
        $no = input('post.no');
        $name = input('post.name');
        $sex = input('post.sex');
        $age = input('post.age');
        $classid = input('post.classid');
        $photo = input('post.photo');
        $data = ['name' => $name, 'sex' => $sex, 'age' => $age,'classid' => $classid, 'photo' => $photo];
            try {
            Db::name('student')->where('no', $no)->update($data);
            $this->success('编辑成功', 'all');
        } catch (PDOException $ex) {
            $this->error('编辑失败,' . $ex->getMessage());
        }
    }
    //删除学生
    public function del($no)
    {
        Db::name('student')->where('no', $no)->delete();
        $this->success('删除成功', 'all');
    }
    //判断学号是否存在
    public function validno()
    {
        $no = input('no');
        $data = Db::name('student')->where('no', $no)->find();
        if ($data) {
            return ['valid'=>false];
        }else{
            return ['valid'=>true];
        }
    }

}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加学生</title>
    <{include file="head"}>
    <style>
        #classes{
            margin-left: -9px;
        }
        #classid{
            width: 102.7%;
        }
        #photo{
            margin-top: 8px;
        }
    </style>
</head>
<body>
<div class="container">
    <{include file="nav"}>
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">添加学生</h3>
        </div>
        <div class="panel-body">
            <form class="form-horizontal" action="{:url('do_add')}" method="post">
                <div class="form-group">
                    <label for="no" class="col-sm-2 control-label">学号</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="no" name="no" placeholder="学号">
                    </div>
                </div>
                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">姓名</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="name" name="name" placeholder="姓名">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">性别</label>
                    <div class="col-sm-10">
                        <label class="radio-inline">
                            <input type="radio" name="sex" value="男" checked> 男
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="sex" value="女"> 女
                        </label>
                    </div>
                </div>
                <div class="form-group">
                    <label for="age" class="col-sm-2 control-label">年龄</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="age" name="age" placeholder="年龄">
                    </div>
                </div>
                <div class="from-group">
                    <label for="classid" class="col-sm-2 control-label" id="classes">班级</label>
                    <div class="col-sm-10">
                        <select id="classid" name="classid" class="form-control">
                            <{volist name="banji" id="row"}>
                            <option value="{$row.classid}">{$row.classname}</option>
                            <{/volist}>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">图片</label>
                    <div class="col-sm-10">
                        <input type="file" name="photo" id="photo"/>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-primary">提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>

<script>
    $(function () {
        $('form').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                no: {
                    message: 'The username is not valid',

                    validators: {
                        notEmpty: {
                            message: '用户名不能为空'
                        },
                        stringLength: {
                            min: 11,
                            max: 11,
                            message: '用户名长度必须是11位'
                        },
                        threshold :  6 ,
                        remote: {
                            url: '{:url("student/validno")}',
                            message: '学号已存在',
                            delay :  2000,
                            type: 'POST'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9_\.]+$/,
                            message: '用户名由数字字母下划线和.组成'
                        }
                    }
                },
                name: {
                    validators: {
                        notEmpty: {
                            message: '姓名不能为空'
                        }
                    }
                },
                age: {
                    validators: {
                        notEmpty: {
                            message: '年龄不能为空'
                        }
                    }
                }
            }
        });
    });
</script>

这是部分添加学生的代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值