模板标签的使用,分页,文件上传

原创2018-11-28 15:16:5560
摘要:总结:本章主要学习到了模板标签的使用,foreach,volist,和数据分页,文件上传的操作,/***********************  Staff.php 控制器 ***************************/ <?php namespace app\index\controller; use app\mode

总结:本章主要学习到了模板标签的使用,foreach,volist,和数据分页,文件上传的操作,

/***********************  Staff.php 控制器 ***************************/
<?php
namespace app\index\controller;
use app\models\Recruit;
use think\Controller;
use app\index\model\Staff as StaffModel;//设置模型类的别名

class Staff extends Controller
{
    //循环标签
    public function demo1()
    {
        $staffs = StaffModel::all(function($query){
            $query->field(['staff_id','name','sex','age','salary']);
            //->where('salary','<',100);
        });
        //dump($staffs);die;
        //模板赋值
        $this->view->assign('staffs',$staffs);

        //渲染模板
        return $this->view->fetch();
    }

    //分页
    public function demo2()
    {
        //分页配置
        $config = [
            'type' => 'bootstrap',
            'var_page' => 'page'
        ];
        //每页显示的数量
        $num = 5;

        //是否是简单分页?就是只是:上一页,下一页
        $simple = false;

        //用模型来获取所有的分页数据:think\Paginate
        $paginate = StaffModel::paginate($num,$simple,$config);

        //渲染分页HTML代码,返回分页变量
        $page = $paginate->render();

        //将分页数据复制给模板
        $this->view->assign('staffs',$paginate);

        //将分页额变量赋值给模板
        $this->view->assign('page',$page);

        //渲染模板
        return $this->view->fetch();
    }

    //文件上传
    public function demo3()
    {
        //渲染模板
        return $this->view->fetch();
    }

    //处理文件上传
    public function demo4()
    {
        // 1,获取文件的信息
        $file = request()->file('file');
        if(is_null($file)){
            $this->error('没有选择任何文件');
        }
        //2,移动文件到服务器上的指定目录:public/uploads
        $res = $file->validate(['ext'=>'jpg,jpeg,png'])->move('uploads');
        //3,对上传进行验证:文件大小,文件类型
        if(false == $res){
            $this->error($file->getError());
        }
        $this->success('上传成功');
    }
}

/*************************  demo2.html 标签使用和翻页  ******************************/

{load href="/static/bootstrap/css/bootstrap.css"}

<div class="container">
   <div class="row">
       <h3 class="text-center">员工信息登记表</h3>
       <div class="col-md-8 col-md-offset-2">
           <table class="table table-bordered table-hover text-center">
               <tr class="info">
                   <td>ID</td>
                   <td>姓名</td>
                   <td>性别</td>
                   <td>年龄</td>
                   <td>工资</td>
               </tr>

               {volist name="staffs" id="staff"}
                   <tr>
                       <td>{$staff.staff_id}</td>
                       <td>{$staff.name}</td>
                       <td>
                           {//$staff.sex}
                           {//性别必须是0或1,才是合法数据}
                           {in name="staff.sex" value="0,1"}
                               {if $staff.sex == 0}
                               男
                               {else /}
                               女
                               {/if}
                           {/in}
                       </td>
                       <td>
                           {//$staff.age}
                           {//between标签}
                           {between name="staff.age" value="20,30"}
                           很年轻
                           {/between}
                           {between name="staff.age" value="31,50"}
                           人到中年
                           {else /}
                           快退休了
                           {/between}
                       </td>
                       <td>{$staff.salary}</td>
                   </tr>
               {/volist}

           </table>
           <div class="text-center">{$page|raw}</div>
       </div>
   </div>
</div>

{load href="/static/jquery/jquery-3.3.1.js"}
{load href="/static/bootstrap/js/bootstrap.js"}

/******************************  demo3.html 文件上传  ******************************/

<h3>文件上传</h3>
<form action="{:url('demo4')}" enctype="multipart/form-data" method="post">
   <input type="file" name="file" /> <br>
   <input type="submit" value="上传" />
</form>

批改老师:天蓬老师批改时间:2018-11-28 15:19:53
老师总结:文件上传是通过file对象完成,这个对象实际上已经封装好了的, 但并没有提供facade调用模式,要注意 还有,上传文件是一个非常危险的操作,一定要有验证. 另外,在实际开发过程,大多是通过富文本编辑器提供的上传功能,注意这个上传接口的编写,原理与上面是一样的,也是在控制器创建一个方法来处理

发布手记

热门词条