数据分页
查询数据时使用数据操作对象的 page 函数即可快速完成分页,使用page函数,查询数据的格式如下:
分页对象结构
db数据['pageTotal'] //页面数总计
db数据['prePage'] //上一页链接
db数据['pageList'] //列表页链接【数组格式】
db数据['nextPage'] //下一页链接
db数据['minPage'] //第一页链接
db数据['maxPage'] //最后一页链接
db数据['currentPage'] //当前页
分页演示
| ID | 姓名 | 年龄 | 性别 |
|---|---|---|---|
| 1 | 夏倩康 | 27 | 女 |
| 2 | 杭馥菊 | 21 | 男 |
| 3 | 邰泰世 | 23 | 男 |
| 4 | 唐青曼 | 25 | 男 |
| 5 | 温士寒 | 16 | 女 |
| 6 | 濮阳炎强 | 23 | 女 |
| 7 | 王妍悦 | 23 | 女 |
| 8 | 谭薇飘 | 23 | 女 |
| 9 | 匡凡芳 | 18 | 男 |
| 10 | 蒋逸晶 | 28 | 男 |
控制器写法
<?php
class indexController extends Mb{
public $dbName = 'user';
public function index(){
$db = db('user');
$this->data = $db->page(10)->fetchAll();
}
}
视图写法
<!doctype html>
<html lang="zh-CN">
<div class="table-responsive">
<table class="table table-borderless table-hover mb-0">
<thead>
<tr>
<th class="wd-20">ID</th>
<th class="text-left">姓名</th>
<th class="wd-20">年龄</th>
<th class="wd-20">性别</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->data['list'] as $rows){?>
<tr>
<td><?php echo $rows['id'];?></td>
<td class="text-left"><?php echo $rows['name'];?></td>
<td><?php echo $rows['age'];?></td>
<td><?php echo $rows['sex'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
<nav aria-label="Table Paging" class="mb-0">
<?php if(count($this->data['pageList']) > 1):?>
<ul class="pagination justify-content-center mb-0">
<li class="page-item"><a class="page-link" href="<?php echo $this->data['prePage']; ?>">上一页</a></li>
<?php foreach($this->data['pageList'] as $k => $v){if($k == $this->data['currentPage']):?>
<li class="page-item active"><a class="page-link" href="<?php echo $v; ?>"><?php echo $k;?></a></li>
<?php else:?>
<li class="page-item"><a class="page-link" href="<?php echo $v; ?>"><?php echo $k;?></a></li>
<?php endif; }?>
<li class="page-item"><a class="page-link" href="<?php echo $this->data['nextPage']; ?>">下一页</a></li>
</ul>
<?php endif; ?>
</nav>
</div>
</html>