使用thinkPHP6写一个复杂的分页模块

该示例展示了如何在ThinkPHP6框架中创建一个分页模块。通过控制器获取用户模型的总记录数,计算总页数和偏移量,然后从数据库中查询用户数据。在模板中,显示用户列表并生成分页导航链接,允许用户在不同页面间切换。
摘要由CSDN通过智能技术生成

以下是一个使用 ThinkPHP6 构建的复杂分页模块的示例代码:

控制器代码:

<?php
namespace app\controller;

use app\model\User;
use think\Controller;
use think\facade\View;

class UserController extends Controller
{
    public function index()
    {
        $userModel = new User();

        // 每页显示的记录数
        $pageSize = 10;

        // 获取用户总数
        $totalCount = $userModel->count();

        // 计算总页数
        $totalPage = ceil($totalCount / $pageSize);

        // 当前页码
        $page = input('page', 1, 'intval');

        // 计算偏移量
        $offset = ($page - 1) * $pageSize;

        // 获取用户列表
        $userList = $userModel->limit($offset, $pageSize)->select();

        // 渲染模板并输出
        return View::fetch('user/index', [
            'userList' => $userList,
            'totalCount' => $totalCount,
            'totalPage' => $totalPage,
            'pageSize' => $pageSize,
            'currentPage' => $page,
        ]);
    }
}

模板代码:

<!-- user/index.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>邮箱</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($userList as $user): ?>
            <tr>
                <td><?php echo $user->id; ?></td>
                <td><?php echo $user->username; ?></td>
                <td><?php echo $user->email; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <div class="pagination">
        <?php if ($currentPage > 1): ?>
        <a href="<?php echo url('user/index', ['page' => $currentPage - 1]); ?>">上一页</a>
        <?php endif; ?>

        <?php for ($i = 1; $i <= $totalPage; $i++): ?>
        <?php if ($i == $currentPage): ?>
        <span class="current"><?php echo $i; ?></span>
        <?php else: ?>
        <a href="<?php echo url('user/index', ['page' => $i]); ?>"><?php echo $i; ?></a>
        <?php endif; ?>
        <?php endfor; ?>

        <?php if ($currentPage < $totalPage): ?>
        <a href="<?php echo url('user/index', ['page' => $currentPage + 1]); ?>">下一页</a>
        <?php endif; ?>
    </div>
</body>
</html>

此示例代码演示了如何使用 ThinkPHP6 实现复杂的分页功能,包括获取总记录数、计算总页数、计算偏移量、查询数据、以及渲染模板并输出分页导航。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用thinkphp5.1和layui实现分页的步骤: 1. 安装layui和thinkphp5.1 在项目根目录下使用composer安装layui和thinkphp5.1: ``` composer require topthink/think:5.1.* -vvv composer require layui/layui ``` 2. 创建控制器和视图文件 在控制器中定义一个方法用于获取数据和渲染视图: ```php namespace app\index\controller; use think\Controller; use think\Db; class Index extends Controller { public function index() { // 获取数据 $list = Db::name('user')->paginate(10); // 渲染视图 $this->assign('list', $list); return $this->fetch(); } } ``` 在视图文件中使用layui的分页组件: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>分页演示</title> <link rel="stylesheet" href="/static/layui/css/layui.css"> </head> <body> <div class="layui-container"> <table class="layui-table"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> </tr> </thead> <tbody> {volist name="list" id="vo"} <tr> <td>{$vo.id}</td> <td>{$vo.name}</td> <td>{$vo.age}</td> </tr> {/volist} </tbody> </table> <div id="page"></div> </div> <script src="/static/layui/layui.js"></script> <script> layui.use(['laypage', 'layer'], function(){ var laypage = layui.laypage; var layer = layui.layer; // 初始化分页组件 laypage.render({ elem: 'page', count: {$list.total}, limit: {$list.list_rows}, curr: {$list.current_page}, jump: function(obj, first){ if(!first){ // 点击分页按钮时触发的回调函数 window.location.href = '?page=' + obj.curr; } } }); }); </script> </body> </html> ``` 3. 实现分页功能 在控制器中获取分页数据: ```php namespace app\index\controller; use think\Controller; use think\Db; class Index extends Controller { public function index() { // 获取当前页码 $page = $this->request->get('page', 1); // 获取分页数据 $list = Db::name('user')->paginate(10, false, ['page' => $page]); // 渲染视图 $this->assign('list', $list); return $this->fetch(); } } ``` 在视图文件中根据当前页码显示对应的数据: ```html <tbody> {volist name="list" id="vo"} <tr> <td>{$vo.id}</td> <td>{$vo.name}</td> <td>{$vo.age}</td> </tr> {/volist} </tbody> ``` 然后在分页组件的回调函数中设置跳转链接: ```js jump: function(obj, first){ if(!first){ // 点击分页按钮时触发的回调函数 window.location.href = '?page=' + obj.curr; } } ``` 这样就可以实现使用thinkphp5.1和layui实现分页功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值