第一零五天上课 PHP TP框架下分页

时间:2024-05-26 11:36:57

控制器代码(TestController.class.php)

<?php
namespace Home\Controller;
use Home\Controller\EmptyController;
class TestController extends EmptyController
{
    public function test()
    {
        $user = D('User');
        $count = $user->count();             //获取数据总条数
        $page = new \Think\Page($count,1);   //实例化分页类
        $page->setConfig('header',"共{$count}条信息");

        //配置分页导航栏显示的模块
        $xianshi="共%TOTAL_ROW%条数据 共%TOTAL_PAGE%页 %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%";
        $page->setConfig('theme',$xianshi);  //配置分页显示模块
        $page->setConfig('first','首页');     //配置分页单行信息(首页)
        $page->setConfig('prev','上一页');    //配置分页单行信息(上一页)
        $page->setConfig('next','下一页');    //配置分页单行信息(下一页)
        $show = $page->show();               //获取分页导航信息

        //获取分页内容($page->firstRow 从第几条开始)($page->listRows 每页显示几条)
        $list = $user->limit($page->firstRow,$page->listRows)->select();
        $this->assign('list',$list);         //注册变量
        $this->assign('show',$show);         //注册变量
        $this->display();                     //调用模板
    }
}

视图代码(Test/test.html)

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分页</title>
</head>
<body>
<table border="1">
<tr>
    <th>账号</th>
    <th>密码</th>
    <th>姓名</th>
    <th>是否激活</th>
</tr>
<foreach name='list' item='i'>
<tr>
    <td><{$i[uid]}></td>
    <td><{$i[pwd]}></td>
    <td><{$i[name]}></td>
    <td><{$i[isok]}></td>
</tr>
</foreach>
</table>
<{$show}>        //分页导航栏
</body>
</html>