分四个步骤
1,控制器
$this->m 是模型
$count =
t
h
i
s
−
>
m
−
>
w
h
e
r
e
(
this->m->where(
this−>m−>where(map)->count();
p
=
g
e
t
p
a
g
e
(
p = getpage(
p=getpage(count,10);
$data =
t
h
i
s
−
>
m
−
>
w
h
e
r
e
(
this->m->where(
this−>m−>where(map)->order(‘sort desc, id asc’)->limit($p->firstRow, $p->listRows)->select();
$this->assign(‘data’, $data);// 赋值数据集
$this->assign(‘count’, $count);
$this->assign(‘page’, $p->show()); // 赋值分页输出,
2,Common公共模块的公共方法function.php写分页样式
<?php /** * TODO 基础分页的相同代码封装,使前台的代码更少 * @param $count 要分页的总记录数 * @param int $pagesize 每页查询条数 * @return \Think\Page */ function getpage($count, $pagesize = 10) { $p = new Think\Page($count, $pagesize); $p->setConfig('header', '- 共%TOTAL_ROW%条记录 每页' . $pagesize . '条 第%NOW_PAGE%页/共%TOTAL_PAGE%页
- '); $p->setConfig('prev', '上一页'); $p->setConfig('next', '下一页'); $p->setConfig('last', '末页'); $p->setConfig('first', '首页'); $p->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%'); $p->lastSuffix = false;//最后一页不显示为总页数 return $p; } **3,导入public静态资源文件到模板中**
其中分页样式page.css如下 ``` .pages a,.pages span { display:inline-block; padding:2px 5px; margin:0 1px; border:1px solid #f0f0f0; -webkit-border-radius:3px; -moz-border-radius:3px; border-radius:3px; } .pages a,.pages li { display:inline-block; list-style: none; text-decoration:none; color:#429842; } .pages a.first,.pages a.prev,.pages a.next,.pages a.end{ margin:0; } .pages a:hover{ border-color:#429842; color: #ffffff; /*font-weight: bold;*/ background:#429842; } .pages span.current{ background:#429842; color:#FFF; font-weight:700; border-color:#429842; } ``` **4,模板中显示分页**
{$page}最终效果图![在这里插入图片描述](https://img-blog.csdnimg.cn/20190721170648174.png)