abstract:總結(jié):本章主要學(xué)習(xí)到了模板標(biāo)簽的使用,foreach,volist,和數(shù)據(jù)分頁(yè),文件上傳的操作,/*********************** Staff.php 控制器 ***************************/ <?php namespace app\index\controller; use app\mode
總結(jié):本章主要學(xué)習(xí)到了模板標(biāo)簽的使用,foreach,volist,和數(shù)據(jù)分頁(yè),文件上傳的操作,
/*********************** Staff.php 控制器 ***************************/ <?php namespace app\index\controller; use app\models\Recruit; use think\Controller; use app\index\model\Staff as StaffModel;//設(shè)置模型類(lèi)的別名 class Staff extends Controller { //循環(huán)標(biāo)簽 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(); } //分頁(yè) public function demo2() { //分頁(yè)配置 $config = [ 'type' => 'bootstrap', 'var_page' => 'page' ]; //每頁(yè)顯示的數(shù)量 $num = 5; //是否是簡(jiǎn)單分頁(yè)?就是只是:上一頁(yè),下一頁(yè) $simple = false; //用模型來(lái)獲取所有的分頁(yè)數(shù)據(jù):think\Paginate $paginate = StaffModel::paginate($num,$simple,$config); //渲染分頁(yè)HTML代碼,返回分頁(yè)變量 $page = $paginate->render(); //將分頁(yè)數(shù)據(jù)復(fù)制給模板 $this->view->assign('staffs',$paginate); //將分頁(yè)額變量賦值給模板 $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('沒(méi)有選擇任何文件'); } //2,移動(dòng)文件到服務(wù)器上的指定目錄:public/uploads $res = $file->validate(['ext'=>'jpg,jpeg,png'])->move('uploads'); //3,對(duì)上傳進(jìn)行驗(yàn)證:文件大小,文件類(lèi)型 if(false == $res){ $this->error($file->getError()); } $this->success('上傳成功'); } }
/************************* demo2.html 標(biāo)簽使用和翻頁(yè) ******************************/
{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,才是合法數(shù)據(jù)}
{in name="staff.sex" value="0,1"}
{if $staff.sex == 0}
男
{else /}
女
{/if}
{/in}
</td>
<td>
{//$staff.age}
{//between標(biāo)簽}
{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>
Correcting teacher:天蓬老師Correction time:2018-11-28 15:19:53
Teacher's summary:文件上傳是通過(guò)file對(duì)象完成,這個(gè)對(duì)象實(shí)際上已經(jīng)封裝好了的, 但并沒(méi)有提供facade調(diào)用模式,要注意
還有,上傳文件是一個(gè)非常危險(xiǎn)的操作,一定要有驗(yàn)證.
另外,在實(shí)際開(kāi)發(fā)過(guò)程,大多是通過(guò)富文本編輯器提供的上傳功能,注意這個(gè)上傳接口的編寫(xiě),原理與上面是一樣的,也是在控制器創(chuàng)建一個(gè)方法來(lái)處理