模板标签,分页,文件上传

Original 2018-12-25 18:44:19 222
abstract:模板标签controllerpublic function demo1() { $staffs = StaffModel::all(function($query){ $query->field(['staff_id','name','sex','age','salar

模板标签

controller

public function demo1()
	{
		$staffs = StaffModel::all(function($query){
			$query->field(['staff_id','name','sex','age','salary']);
				// ->where('salary','<',100);
		});

		$this->view->assign('staffs', $staffs);

		return $this->view->fetch();
	}

模板视图

{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>
				{empty name="staffs"}
					<h3 style="color: red;">当前没有符合条件的数据,请检查~~</h3>
				{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}
			</table>
		</div>
	</div>
</div>

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

分页

controller

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();

		//备注: tp51自带的分页功能很不灵活,更多时候,推荐使用自己写的分页类

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

文件上传

模板视图

<h3>文件上传</h3>
<form action="demo4" method="post" enctype="multipart/form-data">
	<input type="file" name="file">
	<button>提交</button>
</form>

controller

//文件上传
	//1.渲染一个文件上传的表单
	public function demo3()
	{
		return $this->view->fetch();
	}

	//2.处理文件上传
	public function demo4()
	{
		//1.获取上传的文件信息(Request请求对象中的file(),返回文件对象think/File)
		$file = Request::file('file');
		//查看返回的文件对象
		// halt($file);
		
		//2.将文件从临时目录移到到服务器上的指定目录
		if (is_null($file)) {
			$this->error('没有选择任何文件');
		} 

		// $fileInfo=$file->move('uploads');
		
		//1M=1048576Byte,
		$rule = ['size'=>2097152, 'ext'=>'jpg,jpeg,png,gif','type'=>'image/jpeg,image/png,image/gif'];
		if($file->check($rule)) {
			$fileInfo = $file->move('uploads');
			$res = '<h3 style="color:green;">上传成功</h3>文件名是:'.$fileInfo->getSaveName();		
		} else {
			$res = '<h3 style="color:red;">上传失败</h3>'.$file->getError();	
		}			

		return $res;
		// return '上传成功';
	}


Correcting teacher:韦小宝Correction time:2018-12-26 09:17:42
Teacher's summary:上传、分页在实际的项目中基本上哪里有会用到!课后一定要多多练习!

Release Notes

Popular Entries