作业:TP5.1 Foreach & volist & 分页处理 & 文件上传

原创 2018-11-21 23:49:06 922
摘要:Foreach 相关volist 相关 //循环标签 public function demo1(){ $staffs=StaffModel::all(function($query){ $query->field(['staff_id','name','sex','age','salar

Foreach 相关

volist 相关


	//循环标签
	public function demo1(){
		$staffs=StaffModel::all(function($query){
			$query->field(['staff_id','name','sex','age','salary'])->where('salary','<','5500');
		});
		$this->view->assign('staffs',$staffs);
		return $this->view->fetch();
		
	}	
{load href="/static/bootstrap/css/bootstrap.css"}
<div class="containrer">
	<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>
				{//模板中的注释}
				{/*
				  * 	这是一个多行注释
				  */}
				  {//1.foreach标签:类似于原生foreach}
				  {//在当前模板中创建变量,来保存数据表中的数据}
				  {//assign name="staffs" value=":\app\index\model\staff::all()"}
				  {//assign name="staffs" value=":model('staff'):all()"}
				  <!--
				  {foreach $staffs as $staff}
				  	<tr>
				  		<td>{//$staff.staff_id}</td>
				  		<td>{//$staff.name}</td>
				  		<td>{//$staff.sex}</td>
				  		<td>{//$staff.age}</td>
				  		<td>{//$staff.salary}</td>
				  	</tr>
				  	{/foreach}
				  	-->
				  	{//volist name="staffs" id="staff" offset="3" length="5"}
				  	{//volist name="staffs" id="staff" offset="3" length="5"}
				  	{empty name="staffs" }
				  		<h2 style="color:red;">当前数据表为空</h2>
				  		{else /}
				  	{volist name="staffs" id="staff" }
				  		  	<tr>
						  		<td>{$staff.staff_id}</td>
						  		<td>{$staff.name}</td>
						  		<td>{$staff.sex}</td>
						  		<td>{$staff.age}</td>
						  		<td>{$staff.salary}</td>
				  		</tr>
				  	{/volist}
				  	{/empty}
			</table>
		</div>
	</div>
</div>
{load href="/static/jquery/jquery.js"}
{load href="/static/bootstrap/js/bootstrap.js"}

分页

	public function demo2(){
		$config=[
			'type'=>'bootstrap',
			'var_page'=>'page'
		];
		$num=5;
		$simple=false;
		$paginate=StaffModel::paginate($num,$simple,$config);
		//halt($paginate);
		$page=$paginate->render();
		$this->view->assign('staffs',$paginate);
		$this->view->assign('page',$page);
		return $this->view->fetch();
		
	
	}
{load href="/static/bootstrap/css/bootstrap.css"}
<div class="containrer">
	<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}</td>
						  		<td>{$staff.age}</td>
						  		<td>{$staff.salary}</td>
				  		</tr>
				  	{/volist}
				  
			</table>
			{$page|raw}
		</div>
	</div>
</div>
{load href="/static/jquery/jquery.js"}
{load href="/static/bootstrap/js/bootstrap.js"}

上传相关

public function demo4(){
		//return '上传成功';
		//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('上传成功');
		
	}
<h3>文件上传</h3>
<form action="{:url('demo4')}" method="post" enctype="multipart/form-data">
	<input type="file" name="file">
	<button>上传</button>
</form>


批改老师:天蓬老师批改时间:2018-11-22 09:04:40
老师总结:文件上传,其实还有其它的方案,单独通过文件类操作

发布手记

热门词条