模型model/Staff.php
实例
<?php namespace app\index\model; use think\Model; class Staff extends model { protected $table = 'staff'; protected $pk = 'staff_id'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
控制器 controller/staff.php
实例
<?php namespace app\index\controller; use think\Controller; use app\index\model\Staff as StaffModel; use think\facade\Request; /** * */ class Staff extends Controller { public function demo() { $staffs = StaffModel::all(function($query){ $query->field(['staff_id','name','sex','age','salary']); }); $this->view->assign('staffs',$staffs); $config = [ 'type' => 'bootstrap', 'var_page' => 'page', ]; $num = 5; $simple = false; $paginate = StaffModel::paginate($num, $simple, $config); $page = $paginate->render(); $this->view->assign('staffs', $paginate); $this->view->assign('page', $page); return $this->view->fetch(); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
前段 view/staff/demo.html
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>使用foreach和volist标签分别实现模板数据的分页显示</title> {load href="/static/css/bootstrap.css" /} </head> <body> <div class="container"> <div class="row"> <h3 class="text-center">foreach方式员工信息登记录</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> {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} </table> <div class="text-center">{$page|raw}</div> </div> </div> <div class="row"> <h3 class="text-center">volist方式员工信息登记录</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> <div class="text-center">{$page|raw}</div> </div> </div> </div> </body> </html> {load href="/static/jquery/jquery-3.3.1.js" /} {load href="/static/bootstrap/js/bootstrap.js" /}
运行实例 »
点击 "运行实例" 按钮查看在线实例
效果图