Mac_ThinkPHP_配合bootstrap实现分页

1.首先创建一个model

指定表主键

指定表名

model里的代码

<?php

namespace app\common\model;

use think\Model;

class day3 extends Model
{
    //主键
    protected $pk ="id";
    //表名
    protected $table = "tp_articles";
    
}

2.创建一个对应的controller

对应controller的代码

<?php

namespace app\index\controller;

use think\Controller;

use app\common\model\day3;
use think\Db;
use think\db\Query;
use think\db\Where;

class day3Class extends Controller
{
    //分页方法
    public function page(){
        //查询tp_articles的数据 10条一页
        $data = day3::name("tp_articles")->paginate(10);
        //跳转到view里名字叫page的html页面
        return view("index/page",compact("data"));
    }

}

对应view里的html页面代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>明天会更好</title>
    <link rel="stylesheet" href="/static/css/bootstrap.css"/>
    <link rel="stylesheet" href="/static/css/bootstrap-theme.css"/>
</head>
<body>
    <table class="table table-responsive">
        <thead>
        <tr>
            <th>id</th>
            <th>标题</th>
            <th>描述</th>
            <th>内容</th>
            <th>操作</th>
        </tr>
        </thead>

        {foreach $data as $item}
        <tbody>
        <tr>
            <td>{$item.id}</td>
            <td>{$item.title}</td>
            <td>{$item.desn}</td>
            <td>{$item.body}</td>
            <td>
                <div class="btn-group">
                    <button type="button" class="btn btn-group-xs btn-default">修改</button>
                    <button type="button" class="btn btn-group-xs btn-danger">删除</button>
                </div>
            </td>
        </tr>
        </tbody>
        {/foreach}
    </table>
    {$data|raw}
<script src="/static/jquery.min.js"></script>
    <script src="/static/js/bootstrap.js"></script>
    <script src="/static/js/npm.js"></script>
</body>
</html>

展示效果图👇

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章