<?php namespace app\index\controller; use think\Controller; use app\index\model\User as UserModel; //引入model中命名空间里面的类->的别名; class User extends Controller { //模板赋值; public function demo1(){ //通过助手函数调用all(获取所有信息)方法,$query为条件 $users = UserModel::all(function($query){ $query->field('*'); }); //模板赋值; 在视图中也可以模板赋值; // $this->view->assign('users',$users); //模板渲染; return $this->view->fetch(); //默认视图为user.html; } // 分页主要用到的类为paginator.php的一个抽象类,bootstrap.php类继承了这个抽象类,所以实例化bootstrap类即可 // 分页 public function demo2(){ //分页配置; $config = ['type' =>'bootstrap','var_page' =>'page']; //显示数量; $num = 5; //是否为简单分页; $simple = false; //用模型获取分页数据; think\paginate; $pagelist = UserModel::paginate($num,$simple,$config); //渲染分页html代码,(进入下一页的导航); $page = $pagelist->render(); $this->view->assign('users',$pagelist); $this->view->assign('page',$page); 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> <!-- 这行代码相当于模板赋值; --> <!-- {//assign name="users" value=":\app\index\model\User::all()"} {//assign name="users" value=":model('user')::all()"} --> {foreach $users as $key=> $val} <tr> <td>{$val.id}</td> <td>{$val.name}</td> <!-- <td>{$val.sex?'女':'男'}</td> --> <td> {in name="$val.sex" value='0,1'} {if $val.sex==1} 女 {else/} 男 {/if} {/in} </td> <!-- <td>{$val.age}</td> --> <td> {between name='$val.age' value='10,20'} 年轻人 {/between} {between name='$val.age' value='21,30'} 老年人 {/between} </td> <td>{$val.money}</td> </tr> {/foreach} </table> <!-- {$page|raw}$page是html代码,需要用raw转换 --> <div style="text-align: center;">{$page|raw}</div> </div> </div> </div> {load href="/static/jquery-3.3.1.min.js"} {load href="/static/bootstrap/js/bootstrap.js"}